Rewrite to another address showing the entered URL - php

I am trying to redirect:
Entered URL:
http://localhost/Test/Test1/key/val
To the following address, but showing the previous URL:
http://localhost/Test/Test1/Test1.php?key=val
The .htaccess file is at /Test.
I have this, but always get the HTTP error 404 NOT FOUND:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^([^/]*)/([^/]*)/([\w]+)$ [NC]
RewriteRule ^(.*)$ /%1.php?%2=%3 [L]
I have searched for this but could not find a similar problem solved.
I will appreciate any help to make the appropriate modifications to the rules

I see several issues in your rules.
.1 The rewrite condition regex does not match.
.2 If the entered URI has a trailing slash, the script won't get the query string because val becomes val/
.3 There is nothing to prevent a loop.
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)[^/]*/([^/]*)/([^/]*)/([\w]+)/? [NC]
RewriteCond %{REQUEST_URI} !\.php [NC]
RewriteRule .* %1/%2/%2.php?%3=%4 [L]
Hope that's what you need.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^([^/]*)/([^/]*)/([\w]+)$ [NC]
RewriteRule ^(.*)$ /Test1/%1.php?%2=%3 [L]

Related

How to rewrite an url via htaccess

I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]

issue with URL redirection in Apache (mod_rewrite / mod_alias)

There are many issues related to Apache configuration with mod_rewrite and mod_alias on Stack Exchange but I couldn't find an answer to my issue, so here is another question! ;)
I migrated an old website to a new location.
Articles used to be accessible to an URL such as http://xxx/blog/index.php?post/YYYY/MM/DD/title, so there are many links of that form through the existing webpages.
Now, the URL should be http://xxx/post/YYYY/MM/DD/title, so just remove blog/index.php? from the final URL.
I wanted to use mod_alias and a Redirect clause, but I read here that the Redirect clause was interpreted after the RewriteRule clause, which is a problem in my case because I'm already using a RewriteRule condition.
Here is my current .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
I tried to modify it this way:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/index.php? / [R=301,L]
RewriteRule ^(.*)$ index.php?$1
It almost works except:
http://xxx/blog/index.php?post/YYYY/MM/DD/title is redirected/rewritten to http://xxx/blog/?post/YYYY/MM/DD/title (note the ?) so it doesn't work
it looks like this new line messes up a lot of other URLs that do not start with /blog/index.php? such as the backend URL...
Any help will be more than welcome!
Edit (2014-07-16):
If I use the following rule:
RewriteRule ^/?blog/index\.php(.*)$ /$1 [R=301,L]
then going to
http://xxx/blog/index.php?test
takes me to
http://xxx/?test
which is almost correct (the interrogation mark is still a problem)!
But when I try to match the interrogation mark by adding \?:
RewriteRule ^/?blog/index\.php\?(.*)$ /$1 [R=301,L]
then it just stops working... Going there:
http://xxx/blog/index.php?test
just leaves me there!
Why is that?
Try adding this rule to the beginning of your set of rules:
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
so that it looks like:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \?post/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^&\ ]*)
RewriteRule ^index\.php$ /post/%1/%2/%3/%4? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

Why is htaccess returning full path

I am trying the following in htaccess and everything works except one detail: It posts the whole /var/www/project path instead of just giving me what I think I am telling it to give me.
When I type dev.mysite.com/folder
it correctly gives me http://dev.mysite.com/index.php?path=folder, as expected.
However, in the other case, I am expecting this: http://dev.mysite.com/index.php?path=main.php&u=1079
but instead it gives me http://dev.mysite.com/index.php?path=var/www/dev.mysite.com/main.php&u=1079
(example url: 1079.dev.mysite.com (where 1079 is a user profile))
RewriteEngine On
# force non-www domain
RewriteBase /
# Parse the subdomain as a variable we can access in PHP, and
# run the main index.php script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /main.php?u=%1
# Map all requests to the 'path' get variable in index.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/].*[^\?])
RewriteRule ^(.*)$ http://dev.mysite.com/index.php?path=%1 [L,QSA]
Now, I wonder:
1) What am I doing wrong?
2) Is what I want even possible?
Much thanks to anubhava for his help in the comments =) as it inspired the solution.
I switched the first rewrite rule to:
RewriteRule ^(.*)$ /main.php&u=%1
Then the last condition to:
RewriteCond %{REQUEST_URI} ^/([^/]*)$

Send subdomain as post data

Having spent the past few hours looking for a solution, I thought I'd ask here.
I'm looking for a way to process links such as:
http://subdomain.domain.com/page/subpage/
so they are passed to the server as:
http://domain.com/index.php?subdomain=subdomain&page=page&subpage=subpage
which can be taken by index.php to display the correct page information.
I can get the page and subpage to work but have no clue how to get the subdomain. Note: if there is no subdomain, I would like that field to either be left blank or contain a default value.
Any assistance would be much appreciated. :)
Edit:
Just to clarify, I want the first URL to be what the user sees and can input into the address bar and the second URL to be the page that is loaded by the server when the user navigates to the first URL.
My .htaccess file
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
# Remove 'www'
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://resolutiongaming.com/$1/ [L,R=301]
# Subdomain redirect
RewriteCond %{HTTP_HOST} ^resolutiongaming.com$
RewriteRule ^webdev/$ http://webdev.resolutiongaming.com [R=301,L]
RewriteRule ^artwork/$ http://artwork.resolutiongaming.com [R=301,L]
RewriteRule ^music/$ http://music.resolutiongaming.com [R=301,L]
ErrorDocument 404 /error.php
You may try this in one .htacces file in root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^\.]+)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/? [NC]
RewriteRule .* http://domain.com/index.php?subdomain=%1&page=%2&subpage=%3 [R=301,L]
It will redirect permanently:
http://subdomain.domain.com/page/subpage/ or http://www.subdomain.domain.com/page/subpage/
To:
http://domain.com/index.php?subdomain=subdomain&page=page&subpage=subpage
For the rule to work, the incoming URL scheme: /page/subpage/ must be kept.
To replace permanent redirection with silent mapping, remove R=301 from [R=301,L] or replace it with R for temporal redirection.

How can I use mod_rewrite to remove the query string from my URL?

I am new at mod_rewrite and need some help if is possible.
this is my old url
www.mysite.com/web/page.php?c=categoryname
I need to change it at:
www.mysite.com/web/page/categoryname.html
I am using this code but it doesn't work:
RewriteEngine on
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ page.php?c=$1 [L]
How can I fix the above code to correctly rewrite my URLs?
EDIT:
Thanks to Ulrich Palha
i manage to redirect the html files, but css,js etc are not redirect and the website looks rly bad, this is the code i am using now, till i will get the one who will redirect css too
RewriteEngine On
RewriteBase /web/
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
BUMP ANY HELP PLEASE?
Add the following to the .htaccess file in the web directory of your site, replacing the rules you have above.
RewriteEngine On
RewriteBase /web/
#do not process css/js etc
RewriteCond %{REQUEST_URI} \.(css|js|jpg|png) [NC]
#alternatively you could skip all existing files. Comment previous line and uncomment next to do so
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
#redirect www.mysite.com/web/page.php?c=categoryname to
#www.mysite.com/web/page/categoryname.html
#prevent internal redirect
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} (^|&)c=([^&]+)(&|$) [NC]
RewriteRule ^(page)\.php$ $1/%2.html? [NC,L,R=301]
#rewrite www.mysite.com/web/page/categoryname.html to
#www.mysite.com/web/page.php?c=categoryname
RewriteRule ^(page)/([-a-zA-Z0-9]+)\.html$ $1.php?c=$2 [L,NC]
Your regex in the rule is wrong — you are missing /page, adding an optional final slash that you shouldn't be, and forgetting to remove the .html part. Also your RewriteBase shouldn't have a final slash and your rule shouldn't have a leading slash.
It total, it should be this:
RewriteEngine on
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]+)\.html$ page.php?c=$1 [L]

Categories