htaccess seo friendly urls, mod rewrite, redirection - apache

I'm currently trying to make SEO friendly URL's for very specific URL's, not all of them. I've been at this for 48 hours with no luck. My goal is to make http://mydomain.com/index.php?p=g&id=1 look like this; http://mydomain.com/pageone/ - so far I have been able to achieve the redirection thusfar, but it is showing a 404 "The requested URL /pageone/ was not found on this server". My question is how to make it redirect to the virtual directory and not throw a 404.
Please note I want to add a rule for each page id, not a rule that changes everything from index.php?p=g&id=, just each specific link.
Below is my htaccess code:
Options +FollowSymLinks
Options -Multiviews
RewriteOptions MaxRedirects=1
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=g&id=1$ [NC]
RewriteRule ^index\.php$ /pageone/? [r=301,nc]
Any help with this would be GREATLY appreciated.

You redirected the index.php to /pageone/ but did not define what /pageone/ will actually show. You should add the following line to your .htaccess:
RewriteRule ^pageone/?$ index.php?p=g&id=1 [L]
and remove your rule. If you want to keep both RewriteRules, then add the following lines right after RewriteEngine On:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

Related

.htaccess redirect to subdomain url with hash

I'm trying to redirect a site with the following url structure:
https://example.com/2021/about
to
https://2021.example.com/#about
https://example.com/2021/visit
to
https://2021.example.com/#visit
how can i do this?
i tried adding this to the /about directory in the original domain:
RewriteEngine on
RewriteBase /
RewriteRule (.*) https://2021.example.com/#about [R=301,NE, L]
but what i got after the redirect was
https://2021.example.com/#about2021/about
which is not right. any help is appreciated
[EDIT] i only want to apply this to some folders, like /2021/about and 2021/visit
You may use this redirect rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com) [NC]
RewriteRule ^(20\d{2})/(.+?)/?$ https://$1.%1/#$2 [R=301,L,NE]
Make sure this is your topmost rule in .htaccess and you clear your browser cache before testing this new rule.
Could you please try following, written based on your shown samples. Please make sure to clear your browser cache before testing your URLs. Following will only be applied to 2021/about OR 2021/visit uris.
RewriteEngine ON
RewriteCond %{HTTP_HOST} (example\.com) [NC]
RewriteRule ^(2021)/(about|visit)/?$ http://$1.%1/#$2 [R=301,NC,NE,L]

Where is my mod rewrite fail?

I have got url
ipaddr/opensys/base/
and some other similar, like
ipaddr/opensys/base/something.php?olol=yep
I want to remove from url "opensys" and display there "center", i want to see this links working:
ipaddr/center/base/
and
ipaddr/center/base/something.php?olol=yep
I did it with symlinks, but it was not good, because system is very difficult and with symlink some plugins not works, I want to do it with .htaccess only, after it all will be ok.
My htaccess is:
but links, which i want to see is not working, why?
RewriteEngine on
RewriteRule ^/opensys/base/(.*)$ /center/base/$1 [L]
RedirectMatch 301 ^/opensys/base/(.*)$ /center/base/$1
Redirect works good, but i see 404, rewrite rules is not working. Why?
You also need to handle the /center/base/ now in the .htaccess file as you must be handling opensys earlier. Something like this -
RewriteRule ^/opensys/base/(.*)$ /center/base/$1 [R=301]
RewriteRule ^/center/base/(.*)$ /index.php?args=$1 [QSA,L]
You can use this code in root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /opensys/(base/\S*)\s [NC]
RewriteRule ^ /center/%1 [R=301,L,NE]
RewriteRule ^center/(base/.*)$ opensys/$1 [L,NC]

Mod Rewrite Rules Confusion

I am trying to do a few rules on my website, a few in particular
xxxx.com/administration/
xxxx.com/administration/users/
xxxx.com/administration/devices/
xxxx.com/administration/payments/
I'm trying to get it so on the administration page there is some text and links to the other 3 pages but any of the rules I try they just keep showing the same page even though the URL actually changes, so I assume the rules aren't being followed
I am currently trying the following - I have read up a bit on the rules but I will state here I am not an expert on them so please remember that.
RewriteRule administration/ administration.php
RewriteRule administration/users/ administrationusers.php
RewriteRule administration/devices/ administrationdevices.php
RewriteRule administration/payments/ administrationpayments.php
I have also tried the following sort of rule
RewriteRule administration/ adminfiles/administration.php
RewriteRule administration/(.*)/ adminfiles/$1.php
RewriteRule administration/(.*) adminfiles/$1.php
Any help would be appreciated
Thanks
This is a basic .htaccess file you should use.
RewriteEngine On
RewriteBase /
RewriteRule ^administration/?$ adminfiles/administration.php [L]
RewriteRule ^administration/([\w-]+)/?$ adminfiles/administration$1.php [L]
Note you website must be on the root folder (eg. for Wamp : wamp/www/).
If it's in a subfolder, use RewriteBase /myfolder/ (eg. for Wamp : wamp/www/myfolder/)

multiple mod_rewrite rules in .htaccess

I am having difficulty getting several mod_rewrite rules to work together in my .htaccess files. Throughout the enitre site I want to drop the "www." from all URLs. I am using the following at the document root:
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301]
Then, in one folder "/help" I want to do 2 rewrites:
change domain.com/help/1 to domain.com/index.php?question=1
change domain.com/help/category/example to domain.com/index.php?category=example
So in domain.com/help I have the following:
Options +FollowSymLinks
RewriteRule ^([0-9]+)/?$ index.php?question=$1 [NC,L]
RewriteRule ^category/([^/\.]+)/?$ index.php?category=$1 [NC,L]
The above 2 .htaccess files work for:
www.domain.com to domain.com
domain.com/help/1 to domain.com/index.php?question=1
domain.com/help/category/example to domain.com/index.php?category=example
But, this does not work when I need to combine the 2 rewrites to both drop the "www." and to rewrite the subfolders to a url variable. e.g.:
www.domain.com/help/1 to domain.com/index.php?question=1
gives a 500 error.
Where did I go wrong? And, is this best to do with 2 .htaccess files, or can/should the 2 files be combined into 1 .htaccess file at the document root?
It looks like what's happening is the rules in the .htaccess file in the /help folder is getting applied because you're requesting something in that folder, so the parent folder's rules won't get applied. You can have your parent rules passed down if you add a RewriteOptions Inherit in your /help folder's .htaccess:
Options +FollowSymLinks
RewriteOptions Inherit
RewriteRule ^([0-9]+)/?$ index.php?question=$1 [NC,L]
RewriteRule ^category/([^/\.]+)/?$ index.php?category=$1 [NC,L]
However, the inherited rules may not be applied in the order that you're expecting. For example, if you request http://www.domain.com/help/1/ you'll end up getting redirected to http://domain.com/index.php?question=1 which may not be what you want if you are trying to make SEO friendly URLs by hiding the query string.
Your best bet may be to move the stuff in the /help folder into the one in your document root so that you can control the order that the rules will be applied:
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301]
RewriteRule ^help/([0-9]+)/?$ /index.php?question=$1 [NC,L]
RewriteRule ^help/category/([^/\.]+)/?$ /index.php?category=$1 [NC,L]
This ensures the redirect to the non-www domain occurs first, then the /help rules get applied. So when you go to http://www.domain.com/help/1/, you first get redirected to http://domain.com/help/1/ then the help rules get applied and the URI is rewritten to /index.php?question=1.

URL Rewriting using .htaccess

To change the URL /mobiles.php?id=5 to /mobiles/5
The content of .htaccess file is as follows:
Options +FollowSymlinks
RewriteEngine on
RewriteRule /mobiles/$1 ^/mobiles.php?id=([0-9]+)$
But still it is showing /mobiles.php?id=5 in the address bar. Please help. Is there anything else needs to be added in the .htaccess file?
Note:
mod_rewrite module is enabled
I have restarted Apache server after making changes to the .htaccess
file
.htaccess file is in htdocs folder of Apache.
I am using Windows + PHP + Apache + MySQL
This works for me:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^mobiles.php$ /mobiles/%1? [R,L]
If you see this line:
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
I have added rew variable in the query string to prevent Apache to fall in an infinite loop
When Apache execute this line:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
Is to make sure that url has not been rewritten for Apache
If your only concern is that the old url stays in the address bar, and you want this not to happen, try adding an [R] at the end.
RewriteRule ^/mobiles.php?id=([0-9]+)$ /mobiles/$1 [R]
Did you actually see the correct page?
By the way, the rewrite rules generally go the other way. I would be expecting to see something like:
RewriteRule ^/mobiles/([0-9]+)$ /mobiles.php?id=$1
Is your concern one of making sure a URL with query parameters does not show up in the address bar?
If I understand correctly, you want
Internally redirect /mobiles/5 to /mobiles.php?id=5
Also redirect the browser TO /mobiles/5 if a user navigates to /mobiles.php?id=5
For this you need 2 rules one to internally rewrite the URL for 1st case and 2nd for browser redirection.
You can do it like this:
RewriteEngine on
# for internal rewrite
RewriteRule ^/?mobiles/([0-9]+)/?$ /mobiles.php?id=$1 [L]
# for browser redirect
RewriteRule ^/?mobiles\.php\?id=([0-9]+)$ /mobiles/$1/ [R,L]
You are doing the opposite, should be:
RewriteRule ^/something/mobiles/([0-9]+)$ /something/mobiles.php?id=$1