Removing string and date from url with htaccess and rewrite rules - apache

I would like to replace some parts of my url, year-month and also .html url extension.
Basically i want this ( old url )
https://olddomain.com/weblog/archives/2016/06/post-name-example.html
to redirected to
https://newdomain.com/post-name-example/
this url. I have searched stackoverflow and many other sites, tried mixing different answers from different people together. Tried writing my own rules but i couldn't pull it off. Any help would be much appreaciated. Thank you.
I tried this to remove .html extension
#1)redirect /file.html to /file
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NE,L,R]
This to remove date format from url
# REMOVE DATE FROM WP POST PERMALINKS
RedirectMatch 301 ^/wordpress/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://example.com/$4
http://example.com/wordpress/2012/04/12/post-name/
..to elegance:
http://example.com/post-name/
My htaccess file is like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^weblog/archives/[0-9]{4}/[0-9]{2}/(.*)\.html$ https://newdomain.com/$1/ [R=301.L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This worked properly if anybody needs it in the future:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^weblog/archives/[0-9]{4}/[0-9]{2}/([^.]+)\.html$ http://www.newdomain.com/$1/ [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Related

Having trouble with godaddy htaccess file

RewriteEngine On
RewriteBase /
RewriteRule ^/?$ /1 [L]
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^sayfa-([0-9a-zA-Z-_]+) menu-detay.php?sef=$1 [L,QSA]
RewriteRule ^kategori-([0-9a-zA-Z-_]+) kategoriler.php?sef=$1 [L,QSA]
RewriteRule ^urun-([0-9a-zA-Z-_]+)-([0-9]+)$ urun-detay.php?sef=$1&urun_id=$2 [L,QSA]
RewriteRule ^urun-([0-9a-zA-Z-_]+) urun-detay.php?sef=$1 [L,QSA]
RewriteRule ^bize-ulasin$ iletisim.php [NC,L]
RewriteRule ^sitemap.xml$ sitemap.php [NC,L]
</IfModule>
This is my htaccess file and seo links are not working. Im using LINUX hosting and couldnt find anything helpfull.
When i click my seo url on my index like "website.com/product-33v-new-generation" it redirect me to homepage.
RewriteRule ^urun-([0-9a-zA-Z-_]+)-([0-9]+)$ urun-detay.php?sef=$1&urun_id=$2 [L,QSA]
This is my seo url link for products
RewriteRule ^kategori-([0-9a-zA-Z-_]+) kategoriler.php?sef=$1 [L,QSA]
And this one is for categories.
But non of them are working for somehow. I have another linux hosting but they are working just fine.
Any help appericiated.
With your shown samples, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ index.php [L]
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##RewriteRule ^(.*)/?$ index.php [L]
RewriteRule ^ index.php [L]
RewriteRule ^sayfa-([0-9a-zA-Z-_]+) menu-detay.php?sef=$1 [NC,L,QSA]
RewriteRule ^kategori-([0-9a-zA-Z-_]+) kategoriler.php?sef=$1 [NC,L,QSA]
RewriteRule ^urun-([0-9a-zA-Z-_]+)-([0-9]+)$ urun-detay.php?sef=$1&urun_id=$2 [NC,L,QSA]
RewriteRule ^urun-([0-9a-zA-Z-_]+) urun-detay.php?sef=$1 [L,QSA]
RewriteRule ^bize-ulasin/?$ iletisim.php [NC,L]
RewriteRule ^sitemap\.xml/?$ sitemap.php [NC,L]
</IfModule>
Few fixes in OP's tried htaccess Rules file:
You need NOT to use multiple <IfModule mod_rewrite.c> sections as per your shown attempts.
Need not to use multiple RewriteBase, you could mention it in starting of Rules.
Also fixed few Rules flags and regex in few of the other rules here.

htaccess rewrite URL to subdirectory php

I would like to rewrite all URLs, which starts with a /lp/ to a php file in a subdirectory.
This is my current htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^$ /lp/ [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This is the behaviour I wish to realize:
example .org/lp/ => example.org/lp/ (because index.php is there)
example .org/lp/test => example.org/lp/index.php?p=test
example .org/lp/foo => example.org/lp/index.php?p=foo
I've tried this rule and many others, but it doesn't work:
RewriteRule ^/lp/([^/\.]+)/?$ /lp/index.php?p=$1 [L]
Maybe someone has an idea what I'm doing wrong?

Non-www RewriteRule not working on request URI

I need a website to have all URLs NOT have www in front of it. For this, I have found some useful bits of code here that have helped me out a lot. However, nowhere can I find how to properly do it for ALL request URIs. Currently it only works on the domains homepage.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^(.*)$ https://domain.nl%{REQUEST_URI} [L,R=301]
</IfModule>
# END WordPress
Right now www.domain.nl gets rewritten into domain.nl. However, any other like www.domain.nl/anything_else do not get rewritten (also tried adding a / before the %{REQUEST_URI}, did not work).
Any help is appreciated!
With www rule before:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domain\.nl [NC]
RewriteRule ^ https://domain.nl%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

rewrite htaccess from xyz.com/?type=test&s=test2 to xyz.com/test/test2

what i need is for a search thing, so the test2 is changeable and need the link to change too.
how can i do this in .htaccess?
this is what i thought to do.
RewriteRule ^xyz.com/test/(.*)$ ^xyz.com/?type=test&s=$1 [NC,L]
any idea?
Thanks!
my Wordpress .htacess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^([^/]+)/(.*)$ index.php?type=test&s=$1 [NC,L]
</IfModule>
# END WordPress
You will be looking at something like this:
RewriteRule ^([^/]+)/(.*)$ ?type=$1&s=$2 [NC,L]
Not sure it will work without the filename, though. If that's the case point the rewrite to your file that handles requests, e.g.:
RewriteRule ^([^/]+)/(.*)$ index.php?type=$1&s=$2 [NC,L]
You don't include the domain name in the RewriteRule match pattern.

mod_rewrite check public folder before passing to php

What I am trying to achieve (with mod_rewrite) is on request check if a file/directory exists in the public folder, if it does, serve it, if not, route the request to the index.php file. Also if no extension provided in the URL then default to .html.
Below is my folder structure:
/.htaccess
/index.php
/public
/test.html
/test.xml
/a_folder
/index.html
/test.html
So for example here are a few requests and responses:
example.com/test.xml >>> /public/test.xml
example.com/a_folder/ >>> /public/a_folder/index.html
example.com/a_folder/test >>> /public/a_folder/test.html
example.com/not_in_public >>> /index.php
Any pointers on this would be amazing, thanks in advance.
Something like this: (Heavily cribbed from my WordPress .htaccess file)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)^(\.\w+) $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The $1.html line is off the top of my head and may need some tweaking. Note that this is a dumb check that the requested URL ends in a dot followed by one or more word characters.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(|index\.php)$ - [L]
RewriteRule ^public/(.*)$ - [L]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [R]
</IfModule>
This should do the trick.
Basically, if it's / or /index.php it won't do anything, if it's /public/whateveryouwant it won't do anything, if it's anything else it will rewrite to /public/whateveryouwant and check if it's either a file or a directory. If it's not it will redirect to index.php.
Thanks to #jxpx777 and #N1xx1 after a bit of juggling made it work.
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^public/(.*)$ - [L]
# Check public cache
RewriteRule ^(.*)$ public/$1
RewriteRule (.*)/$ $1/index
# Add .html if not extension provided
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^(.*)$ $1.html
# Push through stack in not in cache
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]