I would like to create an htaccess rule that will leave regular urls untouched, but will redirect friendly urls to a page I call display.php with variables.
So if a user enters the address www.mydomain.com/test.php, he will go to test.php.
But if a user enters www.mydomain.com/test or www.mydomain.com/test/, he will go to display.php?view=test
if a user enters www.mydomain.com/test/one/ the server will load display.php?view=test/one
Thanks
I have it working on iis URL rewrite, but something goes wrong when I tried converting the code to mod_rewrite. The attached code causes all pages, even index.php to be redirected to display.php, which is wrong.
RewriteRule ^/display\.php$ {C:1}/{C:2}
RewriteRule ^([^/]+)/([^/]+)/?$ display.php?view=$1&option=$2
RewriteRule ^/display\.php$ {C:1}
RewriteRule ^([^/]+)/?$ display.php?view=$1
Related
Trying to ask my question again and explain it the best I can.
I am running WAMP on windows 10. Apache version 2.4.18. My project is in www folder named hoidja.ee. mod_rewrite is enabled, AllowOverrideis set to All.
Inside that folder I have .htaccess file, where I am trying to accomplish rewrite without redirect and it does not work at all. I know the .htaccess file is working, since I can rewrite index.php to /home/ for example and it is working perfectly fine.
I have a page with all the users listing. You can click on the user, and it will take you to a url:
http://localhost/Hoidja.ee/hoidja.php?user_id=94&username=John
How I would like this to show is
http://localhost/Hoidja.ee/user/John
without any redirection.
What I have tried:
#tried first with only user_id
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule ^hoidja\.php$ /user/%1? [L]
This gave me 404 that /user/94 doesn't exist.
RewriteRule ^user/([0-9]+)/([A-Za-z0-9-]+)/?$ hoidja.php?user_id=$1&username=$2 [QSA]
This doesn't do anything visually. If I enter http://localhost/Hoidja.ee/user/94/John in the url manually, it goes to that page and shows the user data, but if I enter the original url it does not rewrite it. I am using <base> as well.
I have tried all the possible ways but nothing seems to work. How can I accomplish the url rewrite?
You are getting the 404 error because you are rewriting to a non-existent path /user/94 .you will need to redirect /hoidja.php?user_id= to /user/94 and then rewrite /user/94 back to the real location.
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^user_id=([A-Za-z0-9-]+)$
RewriteRule hoidja\.php$ /hoidja.ee/user/%1? [L,R]
RewriteRule ^(?:hoidja\.ee/)?user/(.+)$ /hoidja.ee/hoidja.php?user_id=$1 [L]
I am sure this already exists, but I'm not too sure the terminology to search for exactly what I am looking for.
I have a url like http://www.example.com/members/$variable/achievements and I want all of these page to redirect to one level up, for example http://www.example.com/members/$variable.
I was originally using :
RewriteEngine On
RewriteRule ^members/([\w]*)/achievements/$ http://www.example.com [R=301,L]
which works fine for redirecting the original page to the homepage, but I would rather it go to that variable page.
RewriteEngine On
RewriteRule ^members/([^/]+)/achievements/?$ http://www.example.com/members/$1/ [R=301,L]
This assumes there is something after $variable/achievements (with and without the ending /) and redirect it back to http://www.example.com/members/$variable/
I have been pulling my hair trying to figure this out but nothing is working. I have a webpage at mysite.com/test.php I want to do a simple URL rewrite and change it to mysite.com/testRewrite/ The code to do implement this should be:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
For whatever reason it's just not working. When you go to the new URL - mysite.com/testRewrite/ it works.
However, if you type in the original URL mysite.com/test.php it doesn't change the address to the new URL in the browser.
mod_rewrite won't automatically enforce redirecting browsers from rewritten URLs. The rule you have simply says "if someone asks for /testRewrite/, internally change it to /test.php". It does nothing to handle actual requests for /test.php, which is why when you try to access mysite.com/test.php, it gives you /test.php.
Now, the problem with mod_rewrite is if you simply add that rule:
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
which will redirect the browser when it asks for /test.php to /testRewrite/, the first rule will be applied after the browser redirects, and the URI gets rewritten to /test.php, then it goes back through the entire rewrite engine and the above rule gets applied again, thus redirecting the browser, thus the first rule rewrites to /test.php, and the above rule redirects again, etc. etc. You get a redirect loop. You have to add a condition to ensure the browser *actually requested /test.php and that it's not a URI that's been churned through the rewrite engine:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
This way, after the 301 redirect happens, this rule won't get applied again because the actual request will be GET /testRewrite/ HTTP/1.1, and the condition won't be met.
Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/
How to redirect my url - www.example.com/home.php?rid=888601032
to www.example.com/examplepage.php
i have tried several htaccess codes 7 Im new to htccess, I would appreciate if someone can give me the correct redirect code.
The redirect you need is incredibly simple.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(home\.php)?\?rid=888601032\ HTTP/
RewriteRule ^(home\.php)?$ http://www.example.com/examplepage.php? [R=301,L]
It redirects any root request with the RID within it.
The syntax is:
redirect accessed-file URL-to-go-to
There are 3 parts;
(1) the Redirect command,
(2) the location of the file/directory you want redirected, and
(3) the full URL of the location you want that request sent to.
These parts are separated by a single space and should be on one line.
For example, if you want to redirect users from oldfile.html in the www directory of your account, myaccount, to newpage.html, the syntax should be
redirect /~myaccount/oldfile.html http://www.indiana.edu/~myaccount/newpage.html
Put this in yout .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} rid=888601032
RewriteRule ^home.php examplepage.php [R,L,QSA]
If you dont want the redirect to be visible, leave the [R] flag.
Hope this helps!
Wouldn't be better to use php to do this?
if ((int)$_GET['rid'] == 888601032) // or something like this
header('Location: examplepage.php');
It depends on what you want to do, but it would be more flexible if you want to redirect to a different page depending on the "rid" parameter, or if you want to do some operation (like setting a cookie) before redirecting the user.