htaccess rewrite - apache

I've rebuilt a site using a CMS and I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: ?secc=country_club. For instance, domain.com?secc=country_club.
I would like to either have a rule for each url or have it rewrite the ?secc=country-club to just country-club
This is what I have tried, without any success:
RewriteRule ^secc-([^-]*)$ /?secc=$1 [L]
I think it has something to do with the ? in the url
Also if it helps, I am using joomla and I do have sh404sef.

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^secc=(.+)$
RewriteRule ^(.*) %1? [R,L]
This will redirect http://example.com/?secc=MYPAGE to http://example.com/MYPAGE

I think you meant to write '=' after ^secc:
RewriteEngine on
RewriteRule ^?secc=(.*)$ "$1" [QSA]

Related

.htaccess rewrite for category with pagination

I'm trying to write a mod-rewrite rule to handle pagination links on my site.
I'd like my URL structure to be this https://example.com/category.php?name=category-link to https://example.com/category/category-link [without pagination]
and https://example.com/category.php?name=category-link&page=2 to https://example.com/category/category-link/2 [with pagination]
I've tried the following:
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+) category.php?name=$1&page=$2
https://example.com/category/category-link isn't working
https://example.com/category/category-link/1 is working
2. am i able to redirect localhost/article/4/ to localhost/article/4 [here 4 is an id]
Any guidance would be much appreciated.
Could you please try following, based on your shown samples only. Please make sure you clear your browser cache after placing these rules into your htaccess file.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/category/([0-9a-zA-Z-]+)/?$ [NC]
RewriteRule ^(.*)$ category.php?name=%1 [L]
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+)/? category.php?name=$1&page=$2 [L]
OR you could use it without RewriteCond too. Make sure you are putting either of these NOT both of them please.
RewriteEngine ON
RewriteRule ^category/([0-9a-zA-Z-]+)/?$ category.php?name=$1 [L]
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+)/? category.php?name=$1&page=$2 [L]

mod_rewrite configuration does not work

I have a page with urls like this:
http://example.com/index.php?site=contact
http://example.com/index.php?site=about
So I try to create custom urls like
http://example.com/contact-the-person
http://example.com/cityname/about
to avoid duplicate content the first url need a permanent redirect into the new code.
this is my code:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+index\.php?site=contact[\s?] [NC]
RewriteRule ^/contact-the-person [R=301,L]
RewriteRule ^contact-the-person/?$ index.php?site=contact [L,NC]
Update:
I changed my code into
RewriteEngine on
RewriteRule ^cityname/about$ index.php?site=contact
and it works now. I can open the url with both links
http://example.com/index.php?site=contact
http://example.com/cityname/about
I just need a redirect from the php version to the clean url now, to avoid dublicate content
Get rid of RewriteBase, if your base is / it is redundant and just complicates things. I am not sure what your RewriteCond is doing, but it isn't necessary to do the 2 rewrites you describe in the question, so get rid of it too.
To make /contact-the-person work:
RewriteRule ^/contact-the-person index.php?site=contact [L,NC]
To make /cityname/about work:
RewriteRule ^/cityname/about index.php?site=about [L,NC]
So the complete file:
RewriteEngine On
RewriteRule ^/contact-the-person /index.php?site=contact [L,NC]
RewriteRule ^/cityname/about /index.php?site=about [L,NC]
UPDATE
To also redirect your index.php?site=contact links to the new pretty format, you'll need to do an external redirect, so that the browser actually makes a new request, and the URL in the browser changes. Do that by adding R to the flags. 301 specifies the http response header, and will ensure your link rankings are preserved. For the example you gave, add a new rule:
RewriteRule ^/index.php?site=contact /contact-the-person [L,R=301]

redirect url already rewritten in htaccess

I have written htaccess
my actual url is
https://www.example.com/index.php?route=information/partnership_form
rewritten as
https://www.example.com/for-brands/partner-with-us
which works fine as I have written rule as
`RewriteRule ^for-brands/partner-with-us$ https://www.example.com/index.php?route=information/partnership_form [NC,L]`
but I want redirection if some user visits direclty https://www.example.com/index.php?route=information/partnership_form
to
https://www.example.com/for-brands/partner-with-us
below is my code redirect but I've tried many ways form other links of stackoverflow still I cant find any solution
rewriterule ^index\.php?route=information\/partnership_form(.*)$ /for-sports-brands/partner-with-us$1 [r=301,nc]
You can not manipulate querystring like that. You need to use a RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} ^route=information/partnership_form [NC]
RewriteRule ^index\.php$ /for-brands/partner-with-us? [NC,L,R]
The ? at the end of the rewrite target is important as it discards the old querystrings.

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 link, and disable original

I have a working mod-rewrite.
RewriteEngine On
RewriteRule ^notifications$ /notifications.php [L]
However, I've seen on some sites, their links seems to be all like website.com/login website.com/notifications website.com/profile
I think I have achieved the same results with my mod-rewrite. But how can I prevent users from entering my /notifications.php page like how some other website does?
Thanks!
Add another rule that looks for a direct request for the php file and redirect it to the non php URL:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /notifications\.php
RewriteRule ^ /notifications [L,R=301]