I'm having a hard time getting a permanent redirect to work. I would like this to happen, using regular expressions.
OLD URL: https://example.com/olddir/other_name_here/123456/garbage.jpg
NEW URL: https://example.com/newdir/other-name-here-123456/
Note the change from underscores to dashes and the fact that I'm throwing the extra bits away after the numeric string. I've tried this but it isn't working (page doesn't exist and still getting a 404):
RewriteRule ^/olddir/other_name_here/([0-9]{6})/.+ /newdir/other-name-here-$1/ [R=301,L]
I have a few hundred names in the "other_name_here" directory location, so if I could dynamically change underscores to hyphens that would be good but not necessary. olddir and newdir are actual names and can be hardcoded. What am I doing wrong? Thanks!
Try the following actions:
If not present in your .htaccess add the following 2 lines before your RewriteRule:
Options +FollowSymLinks
RewriteEngine On
Change your regex in the following way by adding a $:
RewriteRule ^/olddir/other_name_here/([0-9]{6})/.+$ /newdir/other-name-here-$1/ [R=301,L]
Try to access to your new URL pages directly and confirm that they are accessible.
Finally, I would recommend this website for details about the .htaccess detailed configuration: https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules
Related
I have a PHP file named as otp.php.
When URL in the URL bar is
http://localhost/college/otp/MTA=/teacher
It should be treated as
http://localhost/college/otp.php?user=MTA=&role=teacher
For this, I created .htaccess file in http://localhost/college/:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^otp/?$ otp.php [NC,L]
RewriteRule ^otp/[A-Za-z-]+/([A-Za-z0-9-]+)/?$ otp.php?user=$1&role=$2 [NC,L]
But, otp.php file says:
Notice: Undefined index: role in C:\wamp\www\college\otp.php on line
11
Notice: Undefined index: user in C:\wamp\www\college\otp.php on line
11
UPDATE
When URL in the URL bar is
http://localhost/college/otp/MTA/teacher
It should be treated as
http://localhost/college/otp.php?user=MTA&role=teacher
How do I solve this problem?
URL: /college/otp/MTA=/teacher
Target: /college/otp.php?user=MTA=&role=teacher
.htaccess: /college/.htaccess
RewriteRule ^otp/[A-Za-z-]+/([A-Za-z0-9-]+)/?$ otp.php?user=$1&role=$2 [NC,L]
Your RewriteRule pattern needs a slight modification to match your example URL, since it will currently fail on the = in MTA=. (Although I've just noticed that the "update" to your question does not show a = in the URL?) This pattern also needs to be capturing in order for the $1 to pick it up.
So, the above directive should read something like:
RewriteRule ^otp/([A-Za-z-]+=)/([A-Za-z0-9-]+)/?$ otp.php?user=$1&role=$2 [NC,L]
This assumes that = always appears at the end of the path segment, as in your initial example (include it inside the character class if it can occur anywhere - although would be a bit confusing). The NC flag is probably unnecessary, unless you also need to allow mixed case versions of otp (unadvisable). You already allow for mixed case in your regex.
UPDATE#1: It seems the second path segment is a base64 encoded string/integer. For this you will need to include digits in the regex and there could be 0, 1 or 2 trailing = characters. There is also no need to match a hyphen. For example:
RewriteRule ^otp/([A-Za-z0-9]+={0,2})/([A-Za-z0-9-]+)/?$ otp.php?user=$1&role=$2 [NC,L]
However, the other problem you seem to be experiencing (and the one which you are actually "seeing") is a conflict with MultiViews (part of mod_negotiation). This needs to be disabled for the above mod_rewrite directive to work (in fact, to do anything). If you are not enabling this in .htaccess then disable it by including the following at the top of your .htaccess file:
Options -MultiViews
If MultiViews is enabled then when you request otp (where a file with the same basename exists which would also return an appropriate mime-type) mod_negotiation issues an internal subrequest for otp.php. The problem here is that this occurs before mod_rewrite, so otp.php ends up being called without any URL parameters.
Aside:
Your code should not be generating these "undefined index" notices. Since this is essentially "user provided data", you should check for it in your script. For example:
$role = isset($_GET['role']) ? $_GET['role'] : null;
RewriteEngine On # Turn on the rewriting engine
Note that Apache does not support line-end comments, so you should remove the # Turn on the rewriting engine text from the first line. (Line-end comments can appear to "work", however, that is just a coincidence with how Apache directives work in general, other times they will result in a 500 internal server error.)
UPDATE#2:
If the URL bar has http://localhost/college/otp.php?user=MTA=&role=teacher, can it be changed to http://localhost/college/otp/MTA/teacher?
Yes this can be done. Although I assume that MTA= should appear in both places? (You have MTA= in the source and MTA in the target, which would presumably corrupt the base64 encoding?) I assume you are already linking to the correct URL internally and this is only to benefit stray requests (search engines, backlinks, etc.?)
You can implement an external redirect before the above rewrite, being careful not to redirect the rewritten URL and triggering a redirect loop. For example:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+={0,2})&role=([A-Za-z0-9-]+)
RewriteRule ^(otp)\.php$ /college/$1/%1/%2 [QSD,R=302,L]
This is basically the reverse of the internal rewrite (that appears later in the .htaccess file). The condition that checks against the REDIRECT_STATUS environment variable ensures that it only triggers for direct requests and not rewritten requests.
Note that since this is an external redirect, you need to include a root-relative URL-path in the substitution argument. ie. include the /college subdirectory. (Or, you can use a relative substitution and set the RewriteBase - although you'd only do this if you have several of these directives.)
$1 is a backreference to the RewriteRule pattern (ie. always otp) and %1 and %2 are backreferences to the preceding CondPattern, ie. the value of the user and role URL parameters respectively.
The QSD flag (Apache 2.4+) discards the original query string from the request.
This tool might help you to write correct expressions for your RewriteRules. Maybe, this expression would give you an idea, where the problems may be:
(.*otp)\/([A-Za-z0-9-]+=)\/([A-Za-z0-9-]+)
RegEx Descriptive Graph
This link helps you to visualizes your expressions for the RewriteRule:
Then, you can write a RewriteRule, maybe something similar to:
<IfModule mod_rewrite.c>
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{HTTP_HOST} localhost [NC]
RewriteRule (.*otp)\/([A-Za-z0-9-]+=)\/([A-Za-z0-9-]+) $1.php?user=$2&role=$3 [NC,L]
</IfModule>
You might want to clear your browser history, every time that you modify your .htaccess file.
I would like to remove the .html extension from my urls, located into specific directory and redirect 301 them.
Here is how the structure looks like:
mysite.com/category/nameofcategory/pagenumber.html
The thing is that nameofcategory and pagenumber could be any letter or number.
Could you please help me with this?
I wouldn't recommend having your content scattered in many html-files in different folders. This becomes very impractical if you for example want to change the layout of your pages.
Storing the content in a database is a much better solution. If that's not possible perhaps the html files could contain only the formatted text content and a back end script could embed that content to a layout when the page is requested.
This requires that the mod_rewrite module is enabled in the Apache configuration.
In both cases all of the requests would be routed through the back end script and the .htaccess might look something like this:
RewriteEngine on
RewriteRule ^category/([^/.]+)/([^/.]+)/?$ index.php?category=$1&page=$2 [L]
This part of the regex: ([^/.]+) matches and captures a string that doesn't contain the characters / or . and is 1 characters long or longer. The captured strings can be referenced with $1, $2 and so on.
Now the pretty urls like mysite.com/category/foo/bar work. In addition we need to define a rule that redirects the old urls ending in ".html". The rule required might look something like this:
RewriteRule ^category/([^/.]+)/([^/.]+).html$ category/$1/$2 [R=301,L]
One thing to remember while testing and adjusting the redirects is that the redirect may get cached in the browser which may lead to confusing results when testing.
To remove the .html extension on the URL and 301 redirect to the extensionless URL you can try the following in the .htaccess in your "specific directory":
RewriteEngine On
RewriteBase /specific-directory
RewriteRule ^(.*)\.html$ $1 [R=301,L]
I would like to restructure some folders on my website, specifically I am want to move what's contained inside "images/" to "images/gallery/", but I don't want to break previous links, so I thought of using htaccess.
I looked up several tutorials and even several questions here on stackoverflow, tried several times, but I can't get the rewrite rule to work.
This is what I have:
RewriteRule ^images/(.*) /images/gallery/$1 [R=301,NC,L]
But when I try to access anything inside /images/ (for example images/test.jpg) it stays into images/test.jpg and doesn't go to images/gallery/test.jpg. So it doesn't seem to have an effect.
Any clue on what I might possibly doing wrong?
Thank you!
Your rule at present will cause a redirect loop since /images/ is present in both source and target URLs and you're not even using anchor $:
You can tweak your regex like this:
RewriteRule ^images/([^/]+)$ /images/gallery/$1 [R=301,NC,L]
Now pattern will match /images/test.jpg but won't match redirected URL /images/gallery/test.jpg due to use of [^/]+ in pattern.
Make sure this rule is first after RewriteEngine On and there is no .htaccess in /images/ folder.
EDIT: If your original path has sub-directories also then use:
RewriteRule ^images/((?!gallery/).+)$ /images/gallery/$1 [R=301,NC,L]
Due to some bad URLs, we generated some links that don't work and I want to redirect them with a 301 redirect to clear up some webmaster tools issues with Google.
So, we have this URL like this:
http://www.site.com/subdomain/z//-products
*Note that subdomain is variable, the rest of the url is static.
As a side note, this URL makes no sense, that's why I want to redirect it. It should be something like this:
http://www.site.com/bedroom/z/12345/bedroom-furniture-products
Anyway, we had these bad URLs being dynamically generated. We've fixed them, but google picked them up and keeps trying to crawl them. I want to create an htaccess rule to 301 redirect them and the issue should wash out eventually.
Here's what I tried with htaccess to no avail:
^(.*)/n//-products/?$ $1 [R=301,B]
I've also tried all kinds of permutations of this and it's not working. I suspected it was an entity escaping issue, but my research led me to add the [B], but that didn't seem to work either. It's like the redirect rule is working, but it's just redirecting to the original page.
What am i missing here?
I believe anubhava is correct, in that there is inconsistency between the sample URL you describe /subdomain/z//-products and the RewriteRule you attempted to apply. Not sure if this is a typo or not. It may even be the case your copy/paste operation actually added the "/n" literally.
Anyhoo, let us presume that you want to make the rule work with /subdomain/z//-products:
RewriteRule ^/([^/]+)/z//\-products/?$ http://www.site.com/$1 [R=301]
See the example 1 slides of this PDF to get the quick first portion. It is much faster than using (.*).
We literally match the z character and the surrounding slashes. We escape the - character, then we do the rest of the URL and optionally match the trailing slash. We use, if memory serves correctly, an "external" style redirect so that the robots re-open a separate HTTP connection, appending the matched backreference, and hand off the status code.
Let me know if that works.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^subdomain/z/-products/?$ /$1 [L,R=302,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
I have a long list of URLs from an old site structure that I need to redirect using RedirectMatch to new URLs on the same domain. The trick is that the old (source) URLs contain a bunch of messy variables. That's not a problem, right? I just put in place some sweet little Regexp statements to handle those variables. That's what I did, and it matches the variables correctly. Sweet!
The problem comes with the second part of the RedirectMatch statement - the destination. RedirectMatch is correctly resolving the old URLs to the new URLs, except that the old variables are appended to the new URL. I want to keep the redirects, but have the destination URL not contain the variables. Here is my code:
RedirectMatch 301 ^/Shop/Category1/Category2/(.*)$ http://www.website.com/garage.html
Actual Redirect URL:
http://www.website.com/garage.html?launch_pg=itemZoomView&launch_sel=1009152&launch_pg_sp=true&title=Pig+Waste+Can
Can anybody point me to what I am doing wrong here? I just want to get rid of those crummy old variables and start fresh.
If by "variables" you mean query string (the launch_pg=itemZoomView&launch_sel=1009152&launch_pg_sp=true&title=Pig+Waste+Can part of your destination URL example), then use this redirect rule:
RedirectMatch 301 ^/Shop/Category1/Category2/(.*)$ http://www.website.com/garage.html?
The only difference -- the ? at the end of new URL. This stops old query string from being copied over as we telling Apache that new URL will have this empty query string.
If you can use mod_rewrite, here is the rule:
# Activate Rewrite Engine
Options +FollowSymLinks
RewriteEngine On
# the rewrite rule
RewriteRule ^Shop/Category1/Category2/(.*)$ http://www.website.com/garage.html? [R=301,L]
This definitely will redirect with empty query string.