How to hide a part of the url using htaccess file - php

I have the following Rewrite rules in my .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !=off
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^([0-9]+)/([a-zA-Z0-9_-]+)$ system.php?id=$1&title=$2 [L]
With this rules I am getting the url as follows.
http://www.example.com/1111/article-title
I want to hide the "1111" from the url while passing that to the system.php as an argument.Also I need to add some text instead of "1111". So my url should be as follows.
http://www.example.com/text-article-title
Is there any way to do this? Please give some insight on this.
Thanks.

Try this
RewriteCond %{REQUEST_URI} !^example.com/1111/article-title [NC]
RewriteRule ^example.com/(.*)$ example.com/text-article-title/$1 [L]
that should do the trick.

Related

htaccess redirect query string to a path

So i have a htaccess rewrite that makes a query string to a path like so..
RewriteRule ^page/(.*)$ page.php?query=$1
This works fine and using the above i can access the page via both page.php?query= and /page/query/ but how can i make it so that if the page is accessed by the page.php?query= method, it automatically redirects to the path version?
I've tried the following but i don't think i wrote it correctly...
RewriteCond %{HTTP_HOST} ^www\.webaddress\.com\/page\.php\?query\=$
RewriteRule ^(.*)$ http://www.webaddress.com/page/$1 [R=301,L]
Any help to fix this would be appreciated. Thanks.
You can use the following
RewriteEngine On
#url redirection from old url to the new one
#redirect /page.php?query=foo to /page/foo
RewriteCond %{THE_REQUEST} /page.php\?query=([^\s]+) [NC]
RewriteRule ^.+$ /page/%1? [L,R]
#url rewiring from new url to the old one
# rewrite /page/foo to /page.php?query=foo
RewriteRule ^page/(.*)$ page.php?query=$1 [L]
Try it before your internal rewrite rule.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)\.php
RewriteCond %{QUERY_STRING} ^(.+)\=(.+)
RewriteRule ^ http://%{HTTP_HOST}/%1/%2/%3? [R=301]

Subdomain URL Redirect dynamically. .htaccess

I want redirect url using .htaccess
like.
abc.website.com to website.com/folder/abc
http://abc.website.com to http://www.website.com/folder/abc
using .htaccess from PHP server
Use below rule,
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www|webmail|help|whm|root)
RewriteCond %{HTTP_HOST} ^(.+?)\.website.com
RewriteRule ^ http://www.website.com/folder/%1 [R=301,L]
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc.website.com
RewriteRule ^(.*)$ http://website.com/folder/abc/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.abc.website\.com
RewriteRule ^(.*)$ http://website.com/folder/%1/$1 [L,NC,QSA]
I hope it will work for you.

Mod Rewrite URL with several parameters

I'm currently having a hard time rewriting a URL. The .htaccess file already contains some rules so I need to add a new rule to the existing ones. The current rule includes redirecting to https:// if http:// was used (except if the URL contains 'php05142013'). Here's the current content of the .htaccess file:
ErrorDocument 404 https://mycreditstatus.co.za/404.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^mysite\.co\.za$ [NC]
RewriteCond %{REQUEST_URI} !^/(php01202015) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mysite\.co\.za$ [NC]
RewriteCond %{REQUEST_URI} !^/(php01202015) [NC]
RewriteRule (.*) https://mycreditstatus.co.za [L,R=301]
What I want to achieve now in addition to the current rules, is to rewrite the URL like https://mysite.co.za/3pv/XnAnr3ABtgo%3D/3irqrHe%2FQJE%3D into https://mysite.co.za/index.php?action=register&auth=1&p=3pv&eid=XnAnr3ABtgo%3D&us=3irqrHe%2FQJE%3D
I've been trying something like ^/([^A-Za-z0-9.)(/\\%-])/([^A-Za-z0-9.)(/\\%-])/([^A-Za-z0-9.)(/\\%-])/([^A-Za-z0-9.)(/\\%-])/?$ index.php?action=register&auth=1&p=$1&eid=$2&us=$3&%{QUERY_STRING} [NC,L] # Process product requests and I'm sure it's wrong. I'm new to mod rewrite and I find it very confusing. Any help would be appreciated. Thanks.
You can use this new rule:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?action=register&auth=1&p=$1&eid=$2&us=$3 [QSA,L]

replacing Login URL to short URL with .htaccess

I have an URL as :
http://mydomain.com/site/?cmd=home
I want to change the above address to http://mydomain.com/home
i am using .htaccess file like this:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /site?cmd=$1 [L]
I am not sure where is the problem?
Thanks in advance
The Rule works the other way around: you want to get from /site/?cmd=home to /home, but with your RewriteRule you are redirecting to /site?cmd= (Probably there is a mistake somewhere?)
If you want to redirect from /site/?cmd=home to /home, like the question states, use the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/site
RewriteCond %{QUERY_STRING} ^cmd=(.*)$
RewriteRule ^(.*)$ /%1/? [R=302,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /site\?cmd=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^([^/.]+)/?$ /site?cmd=$1 [L]

Htaccess giving me too many redirects

I need help modifying my htaccess file. I have:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^phpbb3.*$ http://mydomain.com/phpbb3/ [R=301,L]
RewriteCond %{HTTP_HOST} mydomain.com$
RewriteCond %{REQUEST_URI} !^/wordit
RewriteRule ^(.*)$ http://www.mydomain.com/wordit/$1 [R=302,L]
Originally I only had:
RewriteCond %{HTTP_HOST} mydomain.com$
RewriteCond %{REQUEST_URI} !^/wordit
RewriteRule ^(.*)$ http://www.mydomain.com/wordit/$1 [R=302,L]
Which I need to keep. But I'm not sure how to incorporate the new rule:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^phpbb3.*$ http://mydomain.com/phpbb3/ [R=301,L]
How do I need to format my redirect?
I think this is what is happening: The rewritten URL coming out of your new rule actually matches the rule itself, so it just loops and loops and loops.
A very simple modification might work:
RewriteRule ^phpbb3[^/]+$ http://mydomain.com/phpbb3/ [R=301,L]

Categories