I have been playing with mod_rewrite using .htaccess to translate some directories - for purposes of both improved SEO and also to produce friendlier/more memorable URLs.
The only problem I can't crack at the moment is with trailing slashes. The behavoir I want is that you should be able to access the link with or without a trailing slash, just to cut down on missed traffic.
My real url is as:
http://www.mydomain.com/shipyard/index.php
I would like people to be able to access it via:
http://www.mydomain.com/shipyard/
http://www.mydomain.com/shipyard
http://www.mydomain.com/ships/
http://www.mydomain.com/ships
Of course, the top two are covered because thats actually a real and accessible URL, but I plan to tell Google that the best way to get at the page is using /ships (without having to move directories, break existing links etc).
The best I came up with so far was:
RewriteRule ^ships/$ /shipyard/index.php [L]
RewriteRule ^ships$ /shipyard/index.php [L]
However I just KNOW that i'm using two lines where only one is needed, but whatever I tried, I couldn't get the one! I know i'm missing something incredibly basic and/or obvious, but I need a pointer... Thanks!
RewriteRule ^ships(/)?$ /shipyard/index.php [L]
This means that the slash may or may not be present.
Related
we're doing a migration and I need to redirect several hundred thousand old urls to new locations. I can get a proof of concept working with mod_rewrite but I know this will not scale. I've been reading about RewriteMap and I think it will do what I need but I can't get it to work.
the search strings can appear anywhere in the url making things more complicated.
the rewrite rules that work look like this
RewriteRule "876f7103-73fd-470b-a5bc-584bcd5135da" "https://XXXXX/V/Redirect?oldDirectLink=https://YYYY/Watch/y4L9TeKs"
RewriteRule "y4L9TeKs" "https://XXXXX/V/Redirect?oldDirectLink=https://YYYY/Watch/y4L9TeKs"
I can't figure out how to convert this to rewritemap... any help would be appreciated...
I've looked at many examples here and all over the internet, but I can't seem to find an answer I understand, or that accurately solves my problem. I'm looking to implement a mod_rewrite directive in an .htaccess file that renames a folder to another name but does not show the name in the url bar.
For example (the user clicks a link that directs them to):
theSite.com/folder1/folder2/folder3/
I want them to see (same as above)
theSite.com/folder1/folder2/folder3/
But I want the browser to silently function in this directory
theSite.com/folder1/some_other_folder/folder3/
I am a PHP developer, writing my first web application. I can configure apache, PHP, mysql and use them like a pro. I'm sorry, but I don't understand the syntax for mod_rewrite. I can't seem to grasp it despite looking at many tutorials as I would need to ask questions before I could move onto the next concept. Thank you for your patience.
Your case is pretty run-of-the-mill. You just need to match the static string, plus a (.*) to match everything that follows it and store it into $1, then substitue some_other_folder.
The [L] flag (and absence of the [R] flag) instructs Apache to rewrite internally without redirecting the browser, and to stop here without matching further rules.
RewriteEngine On
RewriteRule ^folder1/folder2/folder3(.*)$ folder1/some_other_folder/folder3$1 [L]
If folder3 itself is part of the "dynamic" portion, that is, anything after folder2 should be silently rewritten into some_other_folder, leave folder3 out of the rule and just capture everything that follows folder2 into $1.
RewriteEngine On
RewriteRule ^folder1/folder2/(.*)$ folder1/some_other_folder/$1 [L]
I would use following
RewriteRule /folder1/folder2/folder3/ /folder1/some_other_folder/folder3/ [L]
I have a bit of a tricky situation that I'm not sure how to go about finishing.
I have the following code which redirects mysite.com/i/somelink/ to mysite.com/view.php?i=somelink
RewriteRule ^/?i/([^/]+)/$ /view.php?i=$1 [L]
The issue is, the code above only works when there is a trailing slash at the end of mysite.com/i/somelink/
I need it to work both when there is, and is not a slash.
The tricky part is that I'm also trying not to use a separate rule to reduce stress on the system, and the rule needs to be written in such a way that it does not interfere with mysite.com/i/somelink.ext, which also exists, and will be embedded in the file (view.php) I'm redirecting too.
I'm probably making it much more complicated than it needs to be, but basically I just need to make the rule above work both with and without trailing slashes.
Any tips are appreciated. Thanks.
Change it to:
RewriteRule ^/?i/([^/]+)/?$ /view.php?i=$1 [L]
I have been asked by our client to convert a site we created into SEO friendly url format. I've managed to crack a small way into this, but have hit a problem with having the same urls in the same folder.
I am trying to rewrite the following urls,
/review/index.php?cid=intercasino
/review/submit.php?cid=intercasino
/review/index.php?cid=intercasino&page=2#reviews
I would like to get them to,
/review/intercasino
/submit-review/intercasino
/review/intercasino/2#reviews
I've almost got it working using the following rule,
RewriteRule (submit-review)/(.*)$ review/submit.php?cid=$2 [L]
RewriteRule (^review)/(.*) review/index.php?cid=$2
The problem, you may already see, is that /submit-review rewrites to /review, which in turn gets rewritten to index.php, thus my review submission page is lost in place of my index page. I figured that putting [L] would prevent the second rule being called, but it seems that it rewrites both urls in two seperate passes. I've also tried [QSE], and [S=1]
I would rather not have to move my files into different folders to get the rewriting to work, as that just seems too much like bad practise. If anyone could give me some pointers on how to differentiate between these similar urls that would be great!
Thanks
(Ref: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)
What I would do, is make /submit-review/ post directly to itself (or a php file) then once submitted redirect from within the PHP file.
It can be hard to force htaccess to maintain post values whilst redirecting etc
My friend found a solution to this one.
RewriteRule review/submit.php - [L]
Will catch the first rewrite and then prevent the next one, worked a treat!
So I'm playing with a script that makes it super easy to mirror images off of the web. The script works great (based off of the old imgred.com source, if you've seen that) problem is, it looks a little clunky when using it.
Currently, in order to use the script, you go to a url like:
http://mydomain.com/mirror/imgred.php?Image=http://otherdomain.com/image.jpg
What I'd like to do is to be able to go to:
http://mydomain.com/mirror/http://otherdomain.com/image.jpg
and have it redirect to the former URL, preferably transparent to the user.
I'm reasonably certain that this can be done via .htaccess with a MOD_REWRITE of some kind, but I'm getting frustrated trying to get that to work.
After messing with this myself, I found out that apache collapses any double slash in the URL before the query part into a single slash, and passes the result to mod_rewrite. Maybe that was giving you problems?
This might work for you (.htaccess in the mirror directory):
RewriteEngine On
RewriteBase /mirror
RewriteRule ^http(s?):/(.*) imgred.php?Image=http$1://$2 [L]
Don't know if your script accepts https addresses as well, so I included that just to be sure