Got stuck in a very basic .htaccess rewrite - .htaccess

<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)

Related

htaccess replace the parameter in the url

I have a URL like this :
https://example.com/coins/1&order_by=today and several categories such as:
https://example.com/coins/1&order_by=new
https://example.com/coins/1&order_by=trending
You can also change the page so it will be 2 instead of 1 etc.. like : https://example.com/coins/2&order_by=today
I would like to change these to have https://example.com/today?page=1 etc instead of this above
I have read many responses on this subject and I searched the web, but unfortunately no solution works.
I tried this but it does not works:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*&)?order_by=new(&.*)?$
RewriteRule ^(.*)$ $1?new/%1/%2 [L]
Try following htaccess Rules, please make sure to place these Rules at top of your htaccess file. Make sure to place your htaccess file inside root folder along with coins folder(not inside coins folder).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/coins/(\d+)&order_by=(\S+)\s [NC]
RewriteRule ^ /%2?page=%1 [R=301,L]

I want to use a RewriteRule in .htaccess to allow subfolders

We have a url mydomain.com/events that needs to include a country code just before th events folder like this mycomain.com/uk/events. The country could be any number of different codes. I want to use a RewriteRule like:
RewriteRule ^([a-zA-Z0-9-/]+)/events/*.* /events/*.*
so any any url like these will work:
mydomain.com/uk/events
mydomain.com/uk/events/index.php
mydomain.com/uk/events/list-events.php?s=12
With your shown samples/attempts, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Place your htaccess file along with your domain folder(not inside it beside it).
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([^/]*)/(?:[^/]*)/([^/]*)(/.*?\.php(?:\?[^=]+=\d+)?)?\s [NC]
RewriteRule ^ %1/%2%3 [L]

How to rewrite url for certain pages in 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.

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]

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Resources