I'd like to set up a redirect/rewrite in my .htaccess that does the following:
A few samples of redirects I'm trying to accomplish:
domain.com/08/04/10/file.php to domain.com/new/file.php
domain.com/06/01/11/file2.php to domain.com/new/file2.php
domain.com/07/12/07/file3.php to domain.com/new/file3.php
You get the idea. I don't even know where to start - there are so many sub-directories and sub-sub-directories that my head is spinning. I want them all to go to /new/filename.php. There are 3 subdirectories I'm trying to redirect; /06/, /07/, and /08/. Each has 2 levels below it, and a few php files in the 3rd level. Any suggestions on how to start?
RewriteRule ^/([0-9]{2})/([0-9]{2})/([0-9]{2})/(.*)$ new/$4?d=$1,$2,$3 [L,R]
This should turn the first into the second.
/01/02/03/myfile.php
/new/myfile.php?d=01,02,03
I've assumed that you need the folder names as parameters in the PHP call, and that the folders will all be numeric, 2 characters, as it looks strangely like a 2-char-per-part data format ... but if that's not the case, let me know.
Related
I'm trying to come up with one or more rewrite rules that will take either a friendly url or a url containing the full query string.
The plan is to create a text-only page by reading in the URL using PHP's loadHTML.
For example:
Input
1. http://www.example.com/disclaimer (http://www.example.com/text/disclaimer on text-only version)
2. http://www.example.com/info/aboutus (http://www.example.com/text/info/aboutus on text-only version)
3. http://www.example.com/news?id=123 (http://www.example.com/text/news?id=123 on text-only version)
Output
1. http://www.example.com/includes/textonly.php?page=disclaimer
2. http://www.example.com/includes/textonly.php?page=info/aboutus
3. http://www.example.com/includes/textonly.php?news?id=123
So on the textonly.php I would use $_GET['page']); for example 1) and 2), and use $_SERVER['QUERY_STRING']; for example 3).
For example 1) and 2), I came up with:
RewriteRule ^text/(.*) includes/textonly.php?page=$1
And for example 3), I came up with:
RewriteRule ^text/(.[?]) /includes/textonly.php [QSA]
They work independantly but not together. Can anyone help?
With guidance from Tom and Michael, this is what I've come up with:
in Htaccess send everything to PHP in the querystring:
RewriteRule ^text/(.*) /includes/textonly.php [QSA,L]
Then in PHP:
$page = 'http://'.$_SERVER['HTTP_HOST'].'/'.str_replace('/text/','',$_SERVER['REQUEST_URI']);
Seems to work for both friendly urls (2 levels deep tested so far) and querystrings.
Hopefully its okay, so I'll go with this as a solution :)
I suggest handing control over to PHP - I wrote an article on this a while ago - http://tomsbigbox.com/elegant-url-rewriting/ - it details how to send the query string to a PHP file that then decides what to do - so if the page exists for example it will load it, otherwise do something else.
I've found that to be the best solution to URL rewriting.
I'd change your rewrite rule to look like this:
RewriteRule ^([^/\.]+)/?([^/\.]+)?/?$ /includes/textonly.php?page=$1&id=$2 [L,NC]
Then slightly modify your Input URLs to look like this:
1. http://www.example.com/disclaimer
2. http://www.example.com/info/aboutus
3. http://www.example.com/news/123
And they would point to these URLs:
1. http://www.example.com/includes/textonly.php?page=disclaimer&id=
2. http://www.example.com/includes/textonly.php?page=info&id=aboutus
3. http://www.example.com/includes/textonly.php?page=news&id=123
The regex above will match anything between the /, not including a / or a .. The second half is optional, so in that rule, you could only go two directories deep.
Controlling all three URLs in the same way will make your logic a little cleaner on the textonly.php page as you do not need to write special logic for the first two URLs compared to the last.
I have changed my site url structure. But, Google indexed urls are giving 404 not found error. Now, I need a .htaccess rewrite rule for,
From url: www.mydomain.com/topic-titles-here-t273.html
To url: www.mydomain.com/sub-folder/topic273.html
(Topic id must be cached and topic title must be removed.)
Some times, like this also,
From url: www.mydomain.com/topic-titles-here-t273-15.html
To url: www.mydomain.com/sub-folder/topic273-15.html
I searched a lot, for about three hours, But couldn't find correct answer. Please help.
I am a little unclear on what you are trying to do but
1
RewriteRule ^([a-z|-]+)(\d+\.html)$ /sub-folder/topic$2 [NC,L]
Would take the second group (just 273.html) and append accordingly, the first group would be
everything before (topic-titles-here-t)
is that what you require? If so the regex could be tidied I just wanted to demonstrate the two groups
update - Ok according to edit the second group just becomes
(\d{3}-\d+\.html)
if 273 used in this example can be longer than 3 nubers if you know exact just amend that number otherwise use +
RewriteRule ^([a-z0-9|-]+)t(\d+\.html)$ /redirect/topic$2 [NC,R=301,L]
RewriteRule ^([a-z0-9|-]+)t(\d+-\d+\.html)$ /redirect/topic$2 [NC,R=301,L]
I tried. I failed. Here's what I want to do:
Using firebug I saw that the GET string in the request header for my stylesheets and other content was being munged by the application (which I can't modify). I think a simple rewrite rule might help but I can't get it to work. Here's what I need:
input: /content/2010/08/forum/styles/xyz/theme/normal.css
output: /forum/styles/xyz/theme/normal.css
input: /content/forum/styles/xyz/template/blah.js
output /forum/styles/xyz/template/blah.js
input: /content/my-own-page/forum/happy.htm
output: /forum/happy.htm
So, whatever comes in IF it contains "forum/" then get rid of what precedes "forum/" and return everything from "forum/", foward.
Something along these lines should probably do the job.
RewriteRule ^.*/forum/(.*)$ /forum/$1
I have been looking around the site a bit, but I didn't find any replies on how to do it the way I want.
What I want is an URL like this:
www.example.com/Projects/"querystring1 - text only"/"querystring2 - 4 digits only"/
to show the page with this URL:
www.example.com/Projects.aspx?Region=querystring1&Zip=querystring2
What I have been trying is the following:
<rewrite url="~/Leje-og-udlejning-arbejdskraft/(.+)/(.+)" to="~/Workers.aspx?Region=$1&zip=$2"/>
But it just takes both of them as one querysting and put them in the Region-querystring.
I'd need to see the rest of your rewrite rules to be sure, but if you have multiple rewrites (i.e. to handle situations where users only specify one of the two query strings) neglecting to add processing="stop" at the end will cause one rule to override the other.
For example, I use the following setup in my web.config file:
<rewriter>
<rewrite url="~/(.+)/(.+)" to="~/Default.aspx?ref=$1&page=$2" processing="stop"/>
<rewrite url="~/(.+)" to="~/Default.aspx?ref=$1" processing="stop"/>
</rewriter>
This will first check to see if 2 query variables are present. If they are, it rewrites them appropriately and stops processing other rules. This is the biggie. Without processing="stop" at the end, I end up passing both as the ref query string. This might be the conflict you're running in to.
I am trying to find a mod rewrite solution to my problem.
I have almost 6 parameters that i am passing to my url and i want to rewrite.
Let me demonstrate an example of 3 parameters (x,y,z)
The url is list.php?x=1&y=2&z=3 so a rule will be
RewriteRule ^list/x/([^/\.]+)/y/([^/\.]+)/z/([^/\.]+)/?$ list.php?x=$1&y=$2&z=$3
So far fine.
The really tricky part (at least for me) is that the parameters x,y,z have no predicted order so the url might be
list.php?z=2&x=4&y=9
OR
list.php?y=2&z=4&x=9
PLUS
the parameters can came not all together
EX
list.php?z=4
OR
list.php?x=7&z=7
OR whatever combination of 3 arguments
So for 3 arguments i have to write all possible combinations (RewriteRule)
So my htaccess is actually 7 pages with 6 parameters and if want to add one more must write the new rules and correct all others
Is there any other way to accomplish it.
FOR EXAMPLE
if mod rewrite each parameter separate but when more than one parameter come from URL to combine the rules for each parameter.
EXAMPLE
RewriteRule parameter x rewrite to x/$1
RewriteRule parameter y rewrite to y/$2
So when both parameters come became x/$1/y/$2
OR some smart way that i dont have to write 7 pages of rules and be easier to modify
I hope its clear
Something like this should work
RewriteRule ^list/(?=(?:x/(\d+))?)(?=(?:y/(\d+))?) /list.php?x=$1&y=$2
Although it will produce empty parameters but that might be OK.