htaccess query rewrite first time - php

I have tried many of the query to / answers from google and stack overflow, but can't find one that works for me.
I have a file users.php, and when I send users.php?user=username, I want that to be users/user/username. I already have the php extension removed in my .htaccess.
What's the best way to do this? Also, help with the proper file structure required would be helpful.
Edit: Here's what I have so far:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# to make `/path/index.php` to /path/
RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.php [NC]
RewriteRule . %1 [NE,R=301,L]
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]

RewriteEngine On
RewriteRule ^users/user/$ users.php?user=$1 [NC,L]

Try this htaccess code
RewriteEngine On
RewriteRule ^users/user/(.*)/$ users.php?user=$1 [L]

Hehe.. We all have the same answers! Try to remove all the code in your .htaccess file and then check this alone, just itself:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^users/user/([a-z0-9-_]+) /users.php?user=$1 [NC]
Use [NC] flag at the very end of the rule to be matched it in a case-insensitive manner. It means, the rule will doesn't care whether letters appear as upper-case or lower-case.
Now if the folder that containing your /users.php is entitled with /html, give this a try:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^users/user/([a-z0-9-_]+) /html/users.php?user=$1 [NC]
Just change the "html" as the name of the folder containing your users.php.
Okay, lets check this rule:
RewriteRule ^users/user/([a-z0-9-_]+) /beta/users.php?user=$1 [NC]
But please always don't forget the RewriteEngine on and to comment what's happening. And the URL that you must be checked is the not existed URLs that we're trying to remap. (Example: domain.com/users/user/name without a slash in the end.)

Related

how to add index.php in the URL through htaccess

Actually I need to add index.php in my application URL through htaccess file.
My URL is like this.
http://localhost:8080/myapp/xyz/abs.html
I need to change this into.
http://localhost:8080/myapp/index.php/xyz/abs.html
Can anyone tell me what i need to be write in htaccess file.
Any Help will be appreciating.
Thanks.
Have this rule in /myapp/.htaccess:
RewriteEngine On
RewriteBase /myapp/
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) index.php/$1 [L]
Presumably an internal rewrite is required, not an external redirect? In which case, try something like the following, using mod_rewrite in your root .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/index\.php/
RewriteRule ^myapp/(.*) /myapp/index.php/$1 [L]
The RewriteCond directive is required to prevent a rewrite loop. (Only rewrite if it doesn't already contain "/index.php/".)
Try this in your htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} /myapp/xyz/([^.]+)\.html [NC]
RewriteRule ^ /myapp/index.php/xyz/%1.html [R,L,NC]

how to remove question mark

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /domain.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ /domain.com/$1.php [L]
am using this code for remove question mark but its not working, trying http://domain.com/details?id=71 to http://domain.com/details/id/71
Please help me where am wrong?
Thanks in advance
I'm not sure what you're trying to accomplish with the first rewrite, but for the second (the last three lines), if the incoming URI is not an actual directory or file, you want to rewrite something like details/id/71 to /details.php?id=71?
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L]
should get you close, assuming there are always 3 fields. In a RewriteRule you don't put the domain name again.
Your code doesn't do anything remotely close to what you want. Your code simply removes the php extension when a request is made explicitly with the extension and adds it back internally. In order to make http://domain.com/details?id=71 get redirected to http://domain.com/details/id/71, you need:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+([^/]+?)(?:\.php|)\?([^=]+)=([^&\ ]+)
RewriteRule ^ /%1/%2/%3? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1?$2=$3 [L]
you want this before your rules that remove the php extension
Assuming your script is called details.php, the following single rule:
RewriteRule details/(.*)/(.*)/$ /details.php?$1=$2
Should rewrite www.domain.com/details/id/71 to www.domain.com/details.php?id=71
You don't need all that other stuff to remove the .php extension.

.htaccess Remove index.php remove query and leave string

I have been working on an .htaccess file. The url I started with was, www.example.com/index.php?page=pagetitle. I want the Link to only show www.example.com/pagetitle.
So far the only thing in my .htaccess file removes the index.php.
Options -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.(php|html?)
RewriteRule ^ /%1 [R=301,L]
Any help would be greatly appreciated, I sadly don't have the knowledge to create these codes on my own.
Thank you in advance.
I will also be editing this question as I figure more of this out, from support of other people etc.
You can try these rules:
Options -MultiViews
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]

.htaccess multiple rules

RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^(v)$ /$1/
# The following rule does the rewrite.
RewriteRule ^(.+)/$ profile.php?name=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ %1?
I'm doing this code above in my .htaccess file, this works fine in my profile.php page because before, my profile.php is displaying this kind of URL:
http://mysite.com/profile.php?name=john
but when I wrote that code this is the result:
http://mysite.com/john/
I also wanted to apply this code to my category.php, so what I did was just to copy the code above and my whole .htaccess file looks like this one:
RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^(v)$ /$1/
# The following rule does the rewrite.
RewriteRule ^(.+)/$ profile.php?name=$1
RewriteRule ^(.+)/$ category.php?cid=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ %1?
RewriteCond %{REQUEST_URI} ^/category.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php
RewriteCond %{QUERY_STRING} cid=([^&]+)
RewriteRule ^category.php$ %1?
But this code gives me a weird result because when I visit
http://mysite.com/john/ the content that i'm seeing is the content of my category.php.
What should I do here? Any help would be greatly appreciated and rewarded!
Thanks!
i have got the problem in ur rule. the problem what i suppose is that your webserver is unable to differentiate the request is of category page / profile page whenever an request comes like
http://mysite.com/john/
so i suggest do one change in either profile page request or in category page request.
simply add http://mysite.com/profile/john/
hope this works out.
To answer the question as it was modified in the comments:
To differentiate between users and categories if these terms appear in the url, you only need one line per rule (untested example):
RewriteEngine On
RewriteBase /
RewriteRule ^user/(\w+)/?$ /profile.php?name=$1 // using only a limited set of chars (letters and underscore) for the user name and an optional / at the end
RewriteRule ^category/(\w+)/?$ /category.php?cid=$1 // using only a limited set of chars for the category (letters and underscore) and an optional / at the end
Looks like I solved it using this one:
RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^user/(!(profile.php))$ user/$1/ [R=301,L]
RewriteRule ^category/(!(category.php))$ category/$1/ [R=301,L]
# The following rule does the rewrite.
RewriteRule ^user/(.+)/$ profile.php?id=$1
RewriteRule ^category/(.+)/$ category.php?id=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^profile.php$ user/%1?
RewriteCond %{REQUEST_URI} ^/category.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^category.php$ category/%1?

.htaccess misreads directories

I have a setup that sets variables for the index page, but it also does the same for directories and I don't want it to do that. Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)$ index.php?name=$1&page=$2
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)/$ index.php?name=$1&page=$2
RewriteRule ^php/$ error/
Now, with this setup, if I type in mysite.com/login it will redirect to the index.php page and set login as the name variable. How do I make it to where it ignores the directories? This is frustrating me and I've looked through this site for over an hour for an answer and can't find a similar question (I might suck at that too, though. haha!).
Also, if you look at the last RewriteRule, you can see that I'm trying to redirect any attempt to access my php/ folder to my error/ folder. This is also not working.
RewriteCond only applies to the immediately following RewriteRule. Also, you can combine lines 3&4, and 5&6 respectively, by using /? on the end, which makes the / optional (regex).
Your file could be similar to this:
RewriteEngine On
#the following line will not rewrite to anything because of "-", & stop rewiting with [L]
RewriteRule ^(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js)/?(.*)$ - [L,NC]
RewriteRule ^php/?(.*)$ error/ [L,NC]
RewriteRule ^([^/]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?name=$1&page=$2 [L]
You may be interested in the Apache Mod_Rewrite Documentation.
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC] !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to either not execute the rule on a directory or a file
More info on the mod_rewrite documentation pages, search for CondPattern

Categories