Friendly URL profile and about using rewrite in htaccess - php

I have this url
http://example.net/test
but i have need
http://example.net/test/info
I have this code in my htaccess ,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+))$ ./id.php?u=$1
how to can i used code with like a link
http://example.net/test/info

Ok. We explain it.
Your htaccess:
Options All -Indexes
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?$1 [L]
Your php:
var_dump(explode('/', explode('?', $_SERVER['REQUEST_URI'])[0]));
You can use this parts for call (with router) to your routines of site. Implementations of controllers, methods, routers - many and different. I do not know what you doing. I only suggest you choose the right way.

Related

PHP redirect to another page with dynamic url and get data [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

apache - rewrite dynamic url into static url on addon domain is not working

I am working on seo of my website,after reading some articles i found out about seo friendly urls so i decided to change all dynamic url into static url.
for eg: most of the urls are like www.abc.com/final.php?id=1&name=name-of-file but i want to change the urls into static urls like www.a.com/name-of-file/1
I tried to edit .htaccess file but its not working.
my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteRule final/id/(.*)/name/(.*)/ final.php?id=$1&name=$2
Try it like this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\d]+)$ final.php?id=$2&name=$1 [QSA,NC,L]
You can use:
Options +FollowSymLinks
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+final\.php\?id=([^\s&]*)&name=([^\s&]*) [NC]
RewriteRule ^ /%2/%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/? final.php?id=$2&name=$1 [NC,L,QSA]

two RewriteRule's for two different but like urls

My .htaccess is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
RewriteRule . index.php [L]
so all requests i receive in my index.php and then parse query string, and one of my pages is "api documentation page" with url http://domain.com/api so i require page about api from templates
and another url is http://domain.com/api.php where i need receive $_GET['action'] and another data with $_POST
so it will work like http://domain.com/api.php?action=start, but i need call it like http://domain.com/api/start or http://domain.com/api/start/ and receive $_GET['action'] - "start"
but this string in my .htaccess doesnt work
RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
You need to keep RewriteCond before every RewriteRule if you want to apply for all URLs or keep them in a separate rule. Try this .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
# ignore all files/directories from all rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.+)$ api.php?action=$1 [L,NC,QSA]
RewriteRule . index.php [L]
Ah, I've had that problem before, can't remember exactly how it went, but this link seems relevant: https://wiki.apache.org/httpd/RewriteFlags/QSA
The only difference with yours is the slash at the beginning to make the paths absolute and you don't need to escape slashes. E.g.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([a-z]*)/? api.php?action=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
But none of those differences seem to be the cause of the problem. As pointed out by nubhava, the RewriteCond's only affect the next rule though.
When faced with this kind of situation though, I usually end up using a router, to do the "rewriting" in PHP.
See also Mod-Rewrite or PHP router?
RewriteEngine On
RewriteBase /
RewriteRule ^api/([^/]*)$ /api.php?action=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
work's, http://htaccess.madewithlove.be/ help's me trying different combinations

Mod Rewrite Htaccess for Fake Directories

I am trying to create fake directories to redirect specific parameters to specific subdirectories as such:
From: www.example.com/username
To: www.example.com/posts?uid=alex
From: www.example.com/p/1234
To: www.example.com/posts?url=1234
It is somehow more complicated example from the rest questions with the same subject on stackoverflow and most answers do not give an explanation on how it works, thus I am not able to come into a conclusion on how I could solve this.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([^/]+)$ /posts?url=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /posts?uid=$1 [L]
First, you need to make all of your links look like this:
www.example.com/username
www.example.com/p/1234
The rewrite rules above will match the /p/1234 (via ^p/([^/]+)$) and /username (via ^([^/]+)$) and internally rewrite them to the /posts URI. Note that this will not change the URL in the browser's location bar, as that is an external redirect. If someone enters www.example.com/posts?uid=alex, the URL will be unchanged.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^p/([^/]+)/?$ /posts?url=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /posts?uid=$1 [L,QSA,NC]

Masking the special pages in mediawiki

We are developing a application using mediawiki. we are not fully aware of mediawiki. We have created a special page as mediawiki/Special:Video. But we do not like to view the word Special: in our url. i.e., we would like to view the url as mediawiki/Video instead of mediawiki/Special:Video. If are not aware of mediawiki functions fully. So kindly let us know how to do this url masking in mediawiki. I have tried like this
Options +FollowSymLinks
RewriteRule ^ajax.php($|/) - [L]
RewriteEngine on
RewriteBase /var/www/
RewriteRule ^mediawiki/video$ mediawiki/Special:Video
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(/?[^/]*)$ /mediawiki/index.php?title=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /mediawiki/index.php?title=$1&actions=$2 [L]
But it is not working
You can rewrite it as follows, no need to use RequestCond
RewriteRule ^mediawiki/Video$ /mediawiki/Special:Video

Categories