How to rewrite url for certain pages in htaccess? - .htaccess

I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.
Lets say the url is: http://localhost/mySite/article.php?article_title=test-title
then I would like to show it as http://localhost/mySite/article/test-title
This is my current htcaccess file:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1
But for some reason it is not redirecting/showing the correct url. I am not getting any errors.
EDIT
Trying to ask my question again and explain it better. Let's say the url is
http://localhost/www.example.com/admin/editUser.php?user_id=126
and I would like to rewrite the url like this:
http://localhost/www.example.com/admin/user/126
then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.
This is my htaccess file atm. It is in the root of www.example.com
#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests
Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
it works fine.
Why is the users redirect not working? What am I doing wrong.

Try it like this for your rule for article url in mysite directory.
RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]
you need to mention start ^ and end $ of string.

Related

Redirect already SEO friendly URL to another SEO friendly URL externally using .htaccess

Part of my .htaccess code is as follows:
RewriteRule ^([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
This basically redirects URL, example.com/XX to example.com/nextlevels_state.php?state=XX and example.com/XX/1 to example.com/nextlevels_state.php?state=XX&page=1 (internally).
Now, I would like to change the URL structure of the URL from example.com/XX to example.com/nextlevels/XX and example.com/XX/1 to example.com/nextlevels/XX/1
and I tried changing .htaccess to as follows:
RewriteRule ^nextlevels/([A-Z]+)*$ nextlevels_state.php?state=$1 [L]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC]
However, as the site urls are already indexed in search engines, I would like to know a way to redirect all the traffic from example.com/XX to example.com/nextlevels/XX (externally) using .htaccess .
Please guide me in this regard. Thank you community :)
Could you please try following, written and tested with shown samples only(improving your already done attempts here). Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for redirect to url example.com/nextlevels/XX.
RewriteRule ^([A-Z]+)$ nextlevels/$1 [R=301]
RewriteRule ^nextlevels/([A-Z]+)/?$ nextlevels_state.php?state=$1 [NC,L]
##Rule for redirect to url example.com/nextlevels/XX/1.
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels/$1/$2 [R=301]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2 [NC,L]

Simple rewrite if a word is present in a url

I have about 100 urls which are like these:
http://example.com/en/contact-us
http://example.com/en/about-us
When the someone clicks on any of them, it should the same page but with a parameter that has been appended
for example :
http://example.com/en/contact-us?language=en
How would i go to solve this? I am tring to write something generic that will do the job for the 100 urls i have.
This is what i have got :
RewriteCond %{REQUEST_URI} /en/ [NC]
RewriteRule ^ %{REQUEST_URI}/?language=en
Internal redirection: URL in the browser doesn't change
RewriteEngine on
RewriteRule ^/?([a-z]+)/([^/]+)/? $1/$2?language=$1 [L]
External redirection: URL in the browser changes to new URL
RewriteEngine on
RewriteRule ^/?([a-z]+)/([^/]+)/? $1/$2?language=$1 [R=301,L]
Please try this
I think you may have forgotten to put a .php file extension at the end?
This will match any two-letter language folder followed by another directory, as long as there is no period.
RewriteRule ^/(\w\w)/([^/]+)/?$ $1/$2.php?language=$1
Be sure to test it with your URLs at regex101.

SEO Friendly URLs & .htaccess

I'm trying to make my URLs SEO friendly but am having issues with my .htaccess
This is how my URL currently looks:
http://www.mysite.com/dns/?domain=stackoverflow.com&submit=Report
But I want it to show like this:
http://www.mysite.com/dns/stackoverflow.com
I've tried so many things I don't know if it's even possible to do so I just wanted to start over with guidance from you guys. The .htaccess I'm working with is in the /dns/ folder. Any help would be greatly appreciated.
Step 1: change all of your links and anchors in all of your pages to look like this: http://www.mysite.com/dns/stackoverflow.com, so when someone clicks a link on your site, they go to a link that looks like http://www.mysite.com/dns/stackoverflow.com instead of http://www.mysite.com/dns/?domain=stackoverflow.com&submit=Report
Step 2: In the htaccess file in your document root, add these rules (above any rules that you may already have):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?dns/(.+)$ /dns/?domain=$1&submit=Report [L,QSA]
Step 3: In the event that a GET method form submission is generating the link, you can add these rules too:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /dns/\?domain=([^&\ ]+)(&submit=Report)?
RewriteRule ^/?dns/?$ /dns/%2? [L,R=301]
See post url rewritin using .HTACCESS
www.mysite.com/dns/?domain=stackoverflow.com&submit=Report
to
www.mysite.com/dns/stackoverflow.com/Report
Try this
Options +FollowSymLinks
RewriteEngine On
RewriteRule \.(js|css|jpe?g|png|gif)$ - [L]
RewriteRule "^dns/([ \w-]+)/([ \w-]+)/?$" dns/?domain=$1&submit=$2 [L,QSA]

Got stuck in a very basic .htaccess rewrite

<h1>' . $name. '</h1>
The above is the reference which points to my profile1.php file. This file is called index.php . It currently displays the urls as this:
http://www.domain.com/interact/profile1.php?id=36
I have tried implementing the .htaccess file to rewrite the url. I tried many combinations and most of them gave a 500 error and some did not rewrite the url.
This is the .htaccess file which I use, it does not make the url to change.
I want the url to look like http://www.domain.com/interact/profile/36
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile1.php/$1 [QSA,L]
</IfModule>
I know this is a very basic question but I seem to stuck in it and have read basic tutorials but am not able to implement it properly.
The files index.php ,profile1.php and .htaccess are in folder named interact.
Tell me any changes required in php or .htaccess files.
It currently displays the urls as this: http://www.domain.com/interact/profile1.php?id=36
...
I want the url to look like http://www.domain.com/interact/profile/36
Step 1:
Change your content to have links like this:
<h1>' . $name. '</h1>
This way, when you click on a link the URL that will appear in the URL address bar is will look like: http://www.domain.com/interact/profile/36
Step 2:
Then you need to use these rules to internally change it back:
RewriteEngine On
RewriteBase /interact/
RewriteRule ^profile/([0-9]*) profile1.php?id=$1 [L,QSA]
In order to point any external links, like google index bots to the new URLs, you'll need to add these as well:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /interact/profile1\.php\?id=([0-9]*)
RewriteRule ^ http://www.domain.com/interact/profile/%2 [L,R=301]
try this
RewriteRule ^interact/profile1.php/(.*)$ interact/profile1.php?id=$1 [L]
i don't know what make QSA modifiers does so i remove it. If you know what, use it)

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

Resources