I want to make an internal redirect with this result:
My old link is like: http://www.example.com/en/some-other-things
My new link should be: http://www.example.com/some-other-things
Basically I want to 'remove' the /en/ and get all the rest of the url in the new url. I've tried this rules in the .htaccess without success:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule "^/en/(.+)" "/$1" [R=301,L]
</IfModule>
Make leading slash optional so that rule works both in Apache config as well in .htaccess files. Use .* instead of .+ to ensure you match example.com/en/ as well. Also there is no need to quote the pattern or target.
RewriteEngine On
RewriteRule ^/?en/(.*)$ /$1 [R=301,L,NC,NE]
Related
I'm trying to match a simple rule to rewrite a url but it's just not matching. I want to redirect
https://example.com/web/thanks/
to
https://example.com/thanks.php
Here's what I've tried
RewriteEngine On
RewriteBase /
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L]
RewriteEngine On
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L]
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/thanks/$ https://example.com/thanks.php [R=302,L]
RewriteEngine On
RewriteBase /
RewriteRule ^web/thanks/$ https://example.com/thanks.php [R=302,L]
and many more tiny variations but none of them are triggering. I tried using this online tool and it returns "This rule was not met". What am I doing wrong?
To rewrite, just use your last rule
RewriteRule ^web/thanks/?$ /thanks.php [L]
with the following changes
no RewriteBase, this is only relevant for some relative URLs
optional trailing slash /?, if you want both /web/thanks or /web/thanks/ to work
no domain name, because this might trigger a redirect instead of a rewrite, see RewriteRule
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.
no R|redirect flag, because this triggers a redirect instead of a rewrite
The pattern ^.*thanks/$ or ^(.*)thanks/$ also works, but it matches any URL ending in thanks/, like /hellothanks/, /areyousurethanks/, /some/path/thanks/, ...
Currently i have this in my .htaccess
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
RewriteRule ^index/^([0-9a-zA-Z-]+)$ index.php?index=$1
RewriteRule ^([0-9a-zA-Z-]+)$ index.php?index=$1
In my browser, when i access like this :
http://domain.com/aboutus
It is working as expected. What i'm trying to do is, how if i have something like this :
http://domain.com/category/sports
What should i put in my .htaccess so that it can read the URL format for the main-category and subcategory ?
You left out the forward slash in your regex.
RewriteRule ^index/([0-9a-zA-Z-\/]+)$ index.php?index=$1 [L]
RewriteRule ^([0-9a-zA-Z-\/]+)$ index.php?index=$1 [L]
Even better, combine the rules into one:
RewriteRule ^(index/)?([0-9a-zA-Z-\/]+)$ index.php?index=$2 [L]
Also added the [L] flag to stop rewriting when a match is found.
I want all my URL with matching pattern as
/released/2013/iron-man
/released/2013/abc-of-death
/released/2012/saw6
to be redirected to
/released/1.php
and I can the name as well.
I am adding this rule to my .htaccess file but its not working
RewriteRule ^released/([0-9]+)/?$ /released/1.php?id=$1 [L]
The trailing question mark matches an optional ending / which is not what you want.
^released/([0-9]+)/iron-man$
or
RewriteRule ^released/([0-9]+)/(.+)$ /released/1.php?id=$1+$2
Problem is that you have $ after second slash but you have movie name after 2nd slash like iron-man etc. Remove $ since you are not matching it.
Make sure that mod_rewrite and .htaccess are enabled 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 /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(released)/([0-9]+)/ /$1/1.php?id=$2 [L,QSA,NC]
A RewriteRule which does not specify a specific external domain is always executed internally. To make it redirect as you ask add [R=301] at the end - or 302, 303 or 307 depending on which kind of redirect you require, but usually 301 is fine.
Besides that, the regular expression you wrote does not allow for extended URLs - remove the trailing $. After that the /? part is moot so you can remove it as well.
The resulting line would read:
RewriteRule ^released/([0-9]+) /released/1.php?id=$1 [L,R=301]
I am trying to redirect old asp pages on a domain name so they link to their respective pages on the CakePHP version (using 1.3). The domain name is the same. These redirects are being added so results in search engines go to the new Cake page.
I have a bunch of Redirect 301's in my /.htaccess file (app/.htaccess and app/webroot/.htaccess are default).
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
Redirect 301 /contacts.asp http://domain/contacts
Redirect 301 /users.asp http://domain/users
</IfModule>
But for some reason, when I go to any of the urls to test the redirects, it appends an extra query string parameter. For example:
Going to: http://domain/contacts.asp results in http://domain/contacts?url=contacts.asp
So the redirect is working but it is appending the url query string parameter. I don't want to completely remove all query string parameters because some of the old asp links have query string parameters that I would also like passed to the corresponding Cake page.
I believe the "url" query string parameter is coming from the app/webroot/.htaccess file as seen:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Note that when I put all my 301 redirects into my Virtual hosts file, the url parameter is not appended. I would like to keep all these redirects in my .htaccess file. How can I prevent this url query string parameter from being appended?
This looks like a case of mod_rewrite and mod_alias stepping over each other. The URI processing pipeline doesn't end when a Redirect directive is applied, it continues through the pipeline and mod_rewrite does its thing. Unfortunately, the end result isn't always what you want. You could just stick with mod_rewrite and drop the Redirect directives.
You can remove the 2 Redirect directives and add these 2 RewriteRules above the ones that map requests to the app/webroot:
RewriteRule ^contacts\.asp$ http://domain/contacts [L,R=301]
RewriteRule ^users\.asp$ http://domain/users [L,R=301]
Ok so i have a url like
domain.com/item/item_id/item_description/page
when i type the link without
/page
on the url it throws a 404 error and i have to type the trailing slash on the url to make it work..
this is my htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^item/([0-9]+)/(.*)/(.*)/?$ item.php?action=item&id=$1&desc=$2&page=$3
i have found this after searching:
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
which kinda solves my problem but how can i make the trailing slash to be optional by the user if the user wants to add it or not so it wont redirect everytime a slash is not found
You can handle the request using one rewriterule.
RewriteRule ^item(?:\.php)/([0-9]+)/([^/]+)?/?([^/]+)?/?$ item.php?action=item&id=$1&desc=$2&page=$3 [L]
Please note I have added (?:\.php) before ^item, just to be sure this rewriterule works, if your webserver for some reason convert request
domain.com/item/...
into
domain.com/item.php/...
Tip: you can see your current rewriterule behavior enabling RewriteLog:
RewriteLogLevel 9
RewriteLog "/var/log/apache2/dummy-host.example.com-rewrite_log"
Be careful do not use this in production.
Use two rewrite rules (one with and one without "page"):
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^item/([0-9]+)/(.*)/?$ item.php?action=item&id=$1&desc=$2
RewriteRule ^item/([0-9]+)/(.*)/(.*)/?$ item.php?action=item&id=$1&desc=$2&page=$3