Apache mod_rewrite from unfriendly query string to a friendly URL - regex

I am new to regex and Apache mod_rewrite, I have tried several ways, it doesn’t convert to URL friendly, I don’t know what I’m doing wrong.
I want to convert from unfriendly query string to a friendly URL on these scenarios:
Scenario 1:
From: https://example.com/page.php?page_url=life-is-good
To: https://example.com/life-is-good/
My .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)/$ /page.php?page_url=$1 [L,R=301]
Scenario 2:
From: https://example.com/resources/?blog.php?blog_url=life-is-good
To: https://example.com/blogs/life-is-good/
My .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(blogs)/(.*)/$ blogs/blog.php?blog_url=$2 [L,R=301]
Any help on this greatly appreciated.

Have it like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(blogs)/blog\.php\?blog_url=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/? [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+page\.php\?page_url=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# ignore all the requests for existing files and directories
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# internal forwards from pretty URL to actual one
RewriteRule ^(blogs)/(.*)/$ $1/blog.php?blog_url=$2 [L,QSA,NC]
RewriteRule ^(.*)/$ page.php?page_url=$1 [L,QSA]

Related

.htaccess: redirect common forms of index url

I want to redirect all common forms of an index url.
My .htaccess file looks like this right now:
## Mod_rewrite in use.
RewriteEngine On
# Redirect .html URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
With my knowledge I created this expression:
## Mod_rewrite in use.
RewriteEngine On
# Redirect index
RewriteCond %{REQUEST_URI} ^(.*)index(\.php|\.html?)?$ [NC]
RewriteRule ^(.*)$ / [R=301,L]
# Redirect .html URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$ [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
And it doesn't work.
This are my questions:
Why can't I use the NC flag there?
RewriteCond %{REQUEST_URI} \.html$ [NC]
When I use RewriteRule in combination with RewriteCond, what is RewriteRule selecting.
Sample URL: https://analytics.google.com/analytics/web/
This is the condition:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$
In my use case is regex selecting the part between string start and .html. But whats the string start?
Why is my first Condition & Rule not working?
I want to select all this urls:
.com/index.html
.com/index.htm
.com/index.php
.com/index
Thx for the support.

Remove the query string from url using htaccess

I need some help with htaccess. Would you be so kind to assist a little bit?
I have a URL like this
https://example.com/index.php?p=application-intelligence
[or]
https://example.com/?p=application-intelligence
Basically the index.php is passed some parameter 'home' to know which page to load i.e. home.php
So I've tried to follow your post on your blog but with not much luck.
So I'd like the final code to be
https://example.com/application-intelligence
Here's my code.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1
Also for the
https://example.com?p=home
I'd like it to be just
https://example.com
You may use these rules:
RewriteEngine on
# handle /?p=home
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=home\s [NC]
RewriteRule ^ /? [R=301,L,NE]
# handle /?p=<something>
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
This is what i have in my apache virtual host conf file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

htaccess rewrite rule with single parameter

When user enters url like
http://example.com/app/abcd123/
I want to show hime page from
http://example.com/app/index.php?param=abcd123
Without changing URL in browser.
I put .htaccess file inside app folder with code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
You can use this code in /app/.htaccess:
RewriteEngine on
RewriteBase /app/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
Try this .htaccess code. It should help you.
Make sure the rewrite base should be the form the root to your present directory.
RewriteBase /app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty
This should be working, I tested it. Let me know if you face any troubles.
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]
Check it there: http://htaccess.mwl.be/ or other online htaccess test service

Remove parameter from URL

I have URL on my localhost like this:
http://localhost/mysite/index.php?id=8
I want to remove index.php?id= from the URL and show it like this:
http://localhost/mysite/8
Or:
http://localhost/mysite/about
With some character in place of integer 8.
You can use this rule in /mysite/.htaccess:
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{THE_REQUEST} /index\.php\?id=([0-9]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [L,QSA]
You're misunderstanding mod-rewrite.
You must use short URLs everywhere in your code (mysite/8), and let your .htaccess redirect these fake URLs to the real one (mysite/index.php?id=8).
You can not expect a .htaccess to beautify your URLs magically.
That being said, the .htaccess you're looking for is :
RewriteEngine On
RewriteBase /mysite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?id=$1 [L]

Htaccess rewrite Single, or multiple $_GET parameter on LINUX base server

Below's url is I need .htaccess rewrite from ?view=home to /view/home
http://example.com/?view=home
And what if my url is quite dynamic with $_GET Parameter that have multiple $_GET?
http://example.com/?view=home&url=this&name=test&id=that
I had try few rewrite module but cant get result that I wanted.
Below is my rewrite engine :
Options +FollowSymlinks
RewriteEngine On
# specific rule
RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)
RewriteRule ^(constant)$ /index.php?view$1 [L]
# general catch-all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
My current web hosting is on Linux Web Hosting, locally running my tested file is on XAMPP environment.
You can this code to redirect from ?view=home to /view/home:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} /\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?$1=$2 [L,QSA]
# specific rule
RewriteCond %{QUERY_STRING} ^([^=]+)=([^&]+)
RewriteRule ^(constant)$ /index.php?view$1 [L]
# general catch-all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]