Is there a way to change URL by htaccess ? ex: - .htaccess

Is there a way to change URL by htaccess ?
ex: current url is www.abc.com/about want change to www/abc.com/test/about
It's not only "about" link but also all links want to add "/test/" before the link

Yes it is possible to achieve that effect using Apache URL Rewriting. Just make the following changes to your .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^test/([a-zA-Z0-9-]*)$ /$1 [NC]
Explanation
The first line of code does as it says, it turns Apache’s Rewrite Engine on. The following 3 lines checks if the requested URL is already a valid file, it only redirects if it isn’t. Meaning , if your website already contains a directory like /test/mydir, then the redirection will not occur. The final line rewrites the URL giving us the desired output.

Related

php dynamic parameters to url with htaccess

I'm trying to make SEO Friendly url by altering php parameters with slash based url.
I want to make
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20
into:
mysite.com/ToplistName/2021-02-20/2021-02-20
I had success with rewriting url but none of my includes are referring to right directory path and I get no css, js and file paths are broken from links.
This is what I have in .htaccess file now:
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ TopList.php?tl=$1&fr=$2&to=$3
Anyone that can help me get this sorted out inside .htaccess file?
Based on your shown samples, could you please try following.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(\d{4}-\d{2}-\d{2})/(\d{4}-\d{2}-\d{2})/?$ TopList.php?tl=$1&fr=$2&to=$3 [L]
I solved this by doing following:
added to html:
<base href="http://example.com/">
added to .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
#1 Changing Full query
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ TopList.php?tl=$1&fr=$2&to=$3
#2 Changing only first landing page
RewriteRule ^([^/.]+)/?$ TopList.php?tl=$1
this gave me following result:
mysite.com/TopList.php?tl=ToplistName ==> mysite.com/ToplistName
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20 ==> mysite.com/ToplistName/2021-02-10/2021-02-20

URL Rewriting using .htaccess

I'm trying to get a url to rewrite using htaccess but can't seem to get it working.
I'm trying to rewrite http://website.com/pages/blog/article.php?article=blog-entry so that it can be entered as http://website.com/pages/blog/blog-entry but i'm getting an error when I try the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^pages/blog/(.+)$ pages/blog/article.php?article=$1 [NC,L]
Can anybody see where i'm going wrong as this just gives me a 404 error. Thanks in advance.
Use this rule inside /pages/blog/.htaccess:
RewriteEngine on
RewriteBase /pages/blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ article.php?article=$1 [QSA,L]
I'm trying to rewrite
http://website.com/pages/blog.php?article=blog-entry so that it can be
entered as http://website.com/pages/blog/blog-entry but i'm getting an
error when I try the following:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond
%{REQUEST_FILENAME}\.php -f
RewriteRule ^pages/blog/(.+)$ pages/blog/article.php?article=$1
[NC,L]
Your wording is confusing, but I believe this is what you mean:
The real url is: http://website.com/pages/blog.php?article=blog-entry
you want to be able to use a 'friendly' url: http://website.com/pages/blog/blog-entry to point to the real url.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pages/blog/(.+)$ /pages/blog/article.php?article=$1 [QSA,L]
The first two tests ask: is this a directory that exists? is this a file that exists? Because article.php is a file, it won't be included in this action, so you won't enter into an endless loop, which is always the risk with incorrectly done rewrite rules.
Take the given url, and use query string append (QSA) to attach the desired data to the actual file that will process the request. This is not a rewrite in that the url the user sees does not change, this only happens internally in apache, which sends the request to the desired target, with the desired information.
You have to test if the file or directory exists because otherwise you'd be applying this rule incorrectly, since it should only be applied when the target does NOT exist. This is basically how all blog/cms 'search engine friendly urls' work, more or less.
Last, since the target is /blog.php?article=blog-entry you can't skip the leading /.
However, it's unclear to me why you'd want the friendly url to be so long, when you can just make it short, and friendlier: like, pages/[article-name]

htaccess redirect domain.com/index.php?page=home to domain.com/home

What is the code for a simple redirect rule in htaccess that make this change :
domain.com/index.php?page=home => domain.com/home
From page=home, home is a dynamically generated page slug.
Also when on domain.com/somepage, output of echo $_GET['page'] should be "somepage"
I hope this will help you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1
The RewriteConds makes sure that you aren't trying to access a real page (if you do so, the url will not be rewritten)
The RewriteRule mets every url like http://domain.com/somepage with or without an ending slash (but not domain.com/somefolder/somepage)
In this example the url displayed to the user doesn't change, if you want so you have to add [R=301] behind the RewriteRule.

mod_rewrite redirect for non-existent URL's

I've got a an old website that I've taken over. New users essentially get a custom page created for them. It was done in a less than fantastic manner. Currently it actually generates a file named for the slug URL created and symbolically links it to a folder called "/main/". Obviously I want to change this. My plan was simply to have it redirect non-existant folders to "/main/" via .htaccess. Currently this is what my .htaccess looks like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|index\.htm)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /main/ [L]
However this generates a redirect loop. What am I missing?
Edit
On that note, I realized I should say I want it to maintain the path that's input. For example if I put http://www.mydomain.com/test_person it should maintain that address, but forward everything to the main folder if that makes sense.
You'll want your rule to not rewrite URLs already beginning with main in order to not have it loop, eg:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^main/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+/(.*)$ /main/$1 [L]
[L] doesn't really mean what you would think [L] should mean in the context of a .htaccess file.
You should be able to get the original url requested from the REQUEST_URI environment variable, but a common thing to do is to pass the slug to the target as a GET variable.
RewriteRule ^([^/]+)/(.*)$ /main/$2?user=$1 [QSA,L]
Or pass everything to a single script which interprets the URL and finds the correct content to return:
RewriteRule ^([^/]+)/(.*)$ /main/index.php?user=$1&path=$2 [QSA,L]

.htaccess redirect conflicts with another htaccess rule that I can not remove

The Information:
So I am fairly new to .htaccess and have being reading a bit.
Basically I want to do this, redirect the url www.example.com/center-pro to www.example.com/pro-hq
(found how here here .htaccess URL redirect)
However the site i am work on already has a .htaccess file in place.
This is what I am adding doing :
Options +FollowSymLinks #already in htaccess file
RewriteEngine On #already in htaccess file
Redirect 301 /center-pro http://www.example.com/pro-hq #line I added
RewriteCond %{REQUEST_FILENAME} !-f #already in htaccess file
RewriteCond %{REQUEST_FILENAME} !-d #already in htaccess file
RewriteRule ^(.*) index.php?idstring=$1&%{QUERY_STRING} #already in htaccess file
The last line seems to be messing with my redirect
So the output from my redirect looks like this :
www.example.com/pro-hq?idstring=center-pro
The Question:
Is there any way to have the redirect rule above and keep the current .htaccees so I don't mess up the current site settings.
** note: Doing a RewriteRule works but i would like to use a redirect so people don't share the wrong url.
You can use mod_rewrite for your external redirection too:
RewriteRule ^center-pro$ http://www.example.com/pro-hq [R=301,L]
This is probably the simplest solution, in place of your current Redirect. The problem is that mod_rewrite performs its work before mod_alias, and while the rewritten URI path is not passed along, the modified query string unfortunately is. You could also condition your RewriteRule to prevent it from processing the path you want handled by mod_alias, but I prefer the first option:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_URI} !^/center-pro
RewriteRule ^(.*) index.php?idstring=$1&%{QUERY_STRING}
Additionally, for your currentRewriteRule, you can just use the QSA flag instead of manually appending the query string to the URL:
RewriteRule ^(.*) index.php?idstring=$1 [QSA]

Resources