I am trying to use RewriteRule to redirect one URL to another (edited with updated examples)
I have the following rules:
RewriteEngine On
RewriteRule old-url new-url [L,R=307]
RewriteCond %{QUERY_STRING} cID\=164
RewriteRule (.*) new-url [R=307,L,QSD]
These both work in my .htaccess file. However, I want these rules in my httpd.conf file (via an include). I have the following:
RewriteEngine On
<Directory /home/staging/web/>
RewriteRule old-url new-url [L,R=307]
RewriteRule /old-url new-url [L,R=307]
RewriteCond %{QUERY_STRING} cID\=164
RewriteRule (.*) new-url [R=307,L,QSD]
</Directory>
The URL redirect doesn't work with or without the leading /, and the query string rule doesn't work either, despite being the same as in the .htaccess file. I am using the <Directory> block as I read this should make it be treated as if it were in a .htaccess file, however it does not seem to.
How can I use RewriteRule in the httpd.conf file and have the same functionality as when used in the .htaccess file?
Related
I want to redirect every requests to my public/index.php file. i've tried
RewriteEngine On
RewriteRule ^(.*)$ public/index.php?url=$1
code that seems fine but its not working. my goal is change url form http://example.com/?url=user/profile/2 to http://example.com/user/profile/2.
my directory structure is
root
public
index.php
vendor
.htaccess
composer.json
To handle URLs like: http://example.com/user/profile/2 please try following Rules sets in your .htaccess file. Place your htaccess rules file into root directory. Please make sure to clear your browser cache before testing your URLs.
Options -MultiViews
RewriteEngine ON
RedirectBase /ApadanaCMS/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ public/index.php?url=$1/$2/$3 [L]
OR try following rules, please make sure either try above OR following rules at a time only.
Above will work for non-existing pages, to make it for any url(like you mentioned in your question) try:
Options -MultiViews
RewriteEngine ON
RedirectBase /ApadanaCMS/
RewriteCond %{REQUEST_FILENAME} !index\.php [NC]
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ public/index.php?url=$1/$2/$3 [L]
heres the solution:
uncoment (remove # ) mod_rewrite.so module in httpd.conf file in apache2 (in my case c:\Apache24\conf\httpd.conf) file. also change AllowOverride none to AllowOverride All in that file under DocumentRoot "${SRVROOT}/htdocs" section.
and this is .htaccess content
RewriteEngine on
RewriteRule ^(.*)$ public/index.php?url=$1 [QSA,L]
RewriteRule ^()$ public/index.php?url=$1 [QSA,L]
however if you need to get static files in your static directory add a new .htaccess file inside static directory with RewriteEngine off content
I want to redirect https://senturia.com.vn/senturia-nam-sai-gon/?gallery=170
to https://senturia.com.vn/senturia-nam-sai-gon/projects/nha-pho-thuong-mai-5mx12m/
I tried this .htaccess file
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} gallery=170 [NC]
RewriteRule (.*) /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L]
</IfModule>
But it's not work
Your configuration looks fine. I highly recommend making sure that mod_rewrite is enabled on the server. Also, make sure that the .htaccess file is located in the correct place. If your are using some CMS with a single index.php file, the .htaccess should sit on the same level as that file. If senturia-nam-sai-gon is an actual directory on your server with its own index.php file, then the .htaccess should sit in that folder.
Here is how I would do it in a CMS:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (?:^|&)gallery=170 [NC]
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC]
</IfModule>
Detailed break-down:
RewriteEngine on simply enables URL rewriting
RewriteBase / sets the base of all URLs (in this case simply /)
RewriteCond %{QUERY_STRING (?:^|&)galler=170 [NC] applies the following rule, if the GET parameters contain gallery=170. The [NC] flag makes the condition ignore upper- and lower-case
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC] redirects /senturia-nam-sai-gon/ to /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/ with a status-code of 301 ("Moved permanently"). The ? at the end of the rule removes the querystring entirely. The L flag makes sure this is the last rule that gets applied. The NC flag does the same as before.
I am having trouble with an htaccess setup. My goal is to have a wildcard sub directory point to the appropriate folder but without changing the URL. I followed some examples here on stackoverflow and elsewhere. I already have my wildcard setup in DNS, Virtual Host is good to go and mod packages. All that is left is the htaccess pointing to the matching folder without changing the url in the browser.
This Works as in I see the domain.com/example/index.html file (But the url changes from example.domain.com to domain.com/example in browser)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,NC,QSA]
</IfModule>
When I swap out the L flag for the P flag so that the url doesnt change. When I do and I try example.domain.com the url does not change as intended but it shows me what I would see if I had went to domain.com instead of what is in the /example folder.
Here is what I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [P,NC,QSA]
</IfModule>
What am I doing wrong?
You are trying to provide the URL in your substitution, which causes the redirection. Try to simply set config as follows:
In VirtualHost
ServerName example.com
ServerAlias *.example.com
DocumentRoot /path/to/your/doc/root
In htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/%1 [NC]
# Please confirm whether you need the next line in your rules.
# I should think that it'd be required
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L,QSA]
I just transfered my Domain to a new Server. Mod_Rewrite is enabled on the new server but unfortunately some RewriteRules don't work, while others do. I haven't changed anything in the .htaccess
So the URL www.mydomain.com/go/10.html should make an internal redirect to www.mydomain.com/go.php?name=10
The snippet in the .htaccess looks like this:
# go.php
RewriteRule ^go$ "$0/" [R=301,L,QSA]
RewriteRule ^go/$ go.php [L,QSA]
RewriteRule ^go/.*?([^\.\/]*)\.html$ go.php?name=$1 [L,QSA]
The $_GET["name"] is not available if I call this url.
Replace your .htaccess code with this.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(go)/([^.]+)\.html$ /$1.php?name=$2 [L,QSA,NC]
What I will use instead of Document_Root in .htaccess??
Following is in my htttp.conf of my linux server.
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.php -f
RewriteRule ^/(.*)(/?)$ /$1.php [L]
RewriteRule ^/([a-zA-Z]+)([a-zA-Z0-9_]{3,15})(/?)$ /profile.php?fairid=$1$2 [L]
RewriteRule ^/([a-zA-Z]+)([a-zA-Z0-9_]{3,15})/([a-z]*)(/?)$ /$3.php?fairid=$1$2 [L]
But now I shifted my domain to shared server. So for running my site properly what changes will be needed in .htaccess in rewrite rules?
My .htaccess is as follows:
RewriteEngine On
RewriteBase /~laborfa2
RewriteCond %/~laborfa2/lf/main/com/%{REQUEST_FILENAME}.php -f
RewriteRule /(.*)(/?)$ /~laborfa2/lf/main/com/$1.php [L]
RewriteRule ^/([a-zA-Z]+)([a-zA-Z0-9_]{3,15})(/?)$ /~laborfa2/lf/main/com/profile.php?fairid=$1$2 [L]
But it is not working. Please suggest the changes will be needed in .htaccess.
REQUEST_FILENAME already is an absolute file system path and prepending DOCUMENT_ROOT is wrong.
Additionally, when using mod_rewrite in a .htaccess file, Apache strips the per-directory path prefix from the requested URI path before testing the rules. And in case of the .htaccess file in the document root of the web server, it’s the leading / that’s stripped of. So your pattern must not begin with a leading slash like in the server or virtual host configuration.
So try this:
RewriteBase /~laborfa2/
RewriteCond %{DOCUMENT_ROOT}~laborfa2/lf/main/com/$1.php -f
RewriteRule (.*)(/?)$ /lf/main/com/$1.php [L]
RewriteRule ^([a-zA-Z]+[a-zA-Z0-9_]{3,15})(/?)$ /lf/main/com/profile.php?fairid=$1 [L]