changing url with get paramater in .htaccess not working - apache

I am trying to change my url using htaccess but it doesn't seem to be working. I want http://example.com/blog_view?id=1 to be changed to http://example.com/blog-id/1/ but it isn't working. I have tried using this.
RewriteEngine On
RewriteRule ^blog-id/([^/]*)/$ /blog_view?id=$1 [L]

You might have to add a RewriteBase directive:
RewriteEngine On
RewriteBase /
RewriteRule ^blog-id/([^/]*)/$ /blog_view?id=$1 [L]
You can test your rules with this tool

You need one redirect and one rewrite rule (already existing):
RewriteEngine On
RewriteCond %{THE_REQUEST} /blog_view\?id=([^\s&]+) [NC]
RewriteRule ^ /blog-id/%1? [R=302,L,NE]
RewriteRule ^blog-id/([^/]+)/?$ blog_view?id=$1 [L,QSA,NC]

Related

How to rewrite .php to .html using .htaccess rules?

I tried and failed.
Original URL
https://www.sitename.com/user/login/index.php
https://www.sitename.com/user/dashboard/index.php
URL that the browser should show
https://www.sitename.com/user/login.html
https://www.sitename.com/user/dashboard.html
This is what I tried and it doesnt work
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php http://%{HTTP_HOST}/user/$1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html http://%{HTTP_HOST}/user/$1.php [L]
Could you please try following. Written and tested with your shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^.]*)\.html/?$ [NC]
RewriteRule ^(.*)$ %1/%2/index.php [L]

.htaccess redirect to change parameter value

I would like to change the URL
/index.php?main_page=product_info&cPath=1&products_id=4
to
/index.php?main_page=product_info&cPath=141&products_id=302
I have tried
RewriteRule ^products_id=4$ products_id=302
but that doesn't seem to match.
My .htaccess file currently contains
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^products_id=302$ products_id=4 [L,R=301]
It would seem like I could use something like
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)products_id=4(&.*)?$
RewriteRule ^index\.php$ /index.php?%1products_id=302%3 [L,R=301]
but that doesn't work reliably. (If I click on a link, it doesn't work, but if I enter that URL in the address bar, it does.)
Try the following rule :
RewriteEngine on
RewriteCond %{THE_REQUEST} /index\.php\?main_page=([^&]+)&cPath=1&products_id=4\sHTTP [NC]
RewriteRule ^ /index.php?main_page=%1&cPath=141&products_id=302 [L,R]

.htaccess URL and directory redirect

This is my first post so excuse me if something it's wrong with my way of asking for help. I've been reading other posts related to my problem but none solved it.
My path:
http://example.com/m/page1
http://example.com/m/page2
...
And so on.
I want to change the /m/pageX to /m/#pageX without hardcoding the URL and without using any additional php scripts.
At the first sight, I've accomplished this task by writing the following .htaccess configuration file:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^[dm]/(.+)$ /m/#$1 [R=302,NE,L,NC]
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+common_([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally forward
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/common_$1.php -f
RewriteRule ^(.+?)/?$ common_$1.php [L]
Is my code the best solution or there is other way to approach my task ?
How can I redirect the /m/pageX to /d/#pageX ?
Thank you
The # sign is not supported in URLs.
This will rewrite /m/pageX to /d/pageX:
RewriteEngine On
RewriteRule ^m/(.*) /d/$1 [L,R=301]
This will rewrite /m/pageX to /d/pageX#example, but #example is fix:
RewriteEngine On
RewriteRule ^m/(.*) /d/$1#example [NE,L,R=301]
[NE] is necessary to keep the hash (#) sign.
This will rewrite /m/pageX to /d/#pageX:
RewriteEngine On
RewriteRule ^m/(.*) /d/#$1 [NE,L,R=301]
Try these rules in your root .htaccess (a level above /m/ and /d/):
RewriteEngine On
RewriteRule ^d/(.+)$ /m/#$1 [NE,NC,L,R=302]
RewriteRule ^(.+)$ /#$1 [NE,L,R=302]
Make sure to remove any /d/.htaccess file.
If for some reason you want to do it from /d/.htaccess then use this rule in /d/.htaccess file:
RewriteEngine On
RewriteRule ^(.+)$ /m/#$1 [NE,NC,L,R=302]

.htaccess is concatenating rules

I have a .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
this is working for http://www.domain.com/frases/13571/
The point is that I need to redirect all HTTP to HTTPS after this rule is already applied.
So I write this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But isn't working like I supposed to. I'm getting this when I perform a HTTP request:
https://www.domain.com/frases/13571/?categoria=frases&id=13571
What is the right code that just change the protocols without modify the others parts of url?
I think that my code is doing 2 requests in the first rules and that is generating this wrong url.
L flag must be applied to your rewrite rule.
You can use this code instead
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/$ /ler.php?categoria=$1&id=$2 [L]
Note:
you don't need to escape slashes (\/ can be /)
you'll need to clear your browser's cache before trying again with this code. Your old rule is now in cache and you won't see the new code working if you don't clear it.
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You rewrite from the original REQUEST_URI, the current is what you have captured, so try:
RewriteRule (.*) https://%{HTTP_HOST}$1 [R=301,L]
or
RewriteRule (.*) https://$1 [R=301,L]
If not working, rewrite log could be useful.

Mod_rewrite - url rewriting isn't working

I've been trying to get the mod_rewrite in my .htaccess file to work for the past few hours but I can't seem to pull it off. The .htaccess files definitely work on my server because I've used them before.
I've tried to rewrite it with this generator and testing it with this tool as well as on my server.
URL
I need to turn
http://someurl.com/news-detail.html?id=10&news=this-is-an-article
into
http://someurl.com/news/10/this-is-an-article
or
http://someurl.com/10/news/this-is-an-article
Rewrite
I've tried:
RewriteEngine On
RewriteRule ^([^/]*)/news/([^/]*)$ /news-detail.html?id=$1&news=$2 [L]
and
RewriteEngine on
RewriteRule /(.*)/news/(.*) news-detail.html?id=$1&news=$2 [L]
and
RewriteEngine on
RewriteRule news/([0-9]+)/([a-zA-Z_]+)$ news-detail.html?id=$1&news=$2 [L]
and many others.
I hope someone can help me out here..
Kindly check/add RewriteBase in your .htaccess file.
Also check if htaccess is allowed to rewrite or not.
Syntax is below
RewriteEngine on
RewriteBase /flodername/
RewriteRule ^urlinbrowser/(.*)/(.*)$ filename.php?first_param=$1&secondparam=$2 [L]
Keep your rules like this in root .htaccess:
RewriteEngine On
RewriteBase /dev/hoig/
RewriteCond %{THE_REQUEST} /news-detail\.html\?id=([^\s&]+)&news=([^\s&]+) [NC]
RewriteRule ^ %1/news/%2? [R=302,L]
RewriteRule ^([^/]+)/news/([^/]+)/?$ news-detail.html?id=$1&news=$2 [L,NC,QSA]
This will support /10/news/this-is-an-article URI structure for pretty URLs.