change permalink through .htaccess - php

i want to change my permalink example www.testing.com/1234/"titlename" instead of www.testing.com/1234/, how to do i change it through .htaccess? Below are my .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /clients/ohmynews/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?article/([a-zA-Z0-9_-]+)$ detail.php?did=$1
RewriteRule ^/?([a-zA-Z0-9_-]+)$ listing.php?lid=$1
RewriteRule ^/?search/([a-zA-Z0-9_-]+)$ search.php?keys=$1
DirectoryIndex index.php
ErrorDocument 404 http://wipstage.com/clients/ohmynews/web/
</IfModule>

Include this line into your htaccess file:
RewriteRule ^([0-9]+)/([A-Za-z0-9._-]+)/?$ detail.php?id=$1&title=$2
As i do not know your actual running file path, so make sure you put your original running file path in my code.
Or let me know your file name with location, i will edit my answer.

Related

Make a friendly URL with 2 parameters on PHP with Apache

So I am in a bit of a soup here.
I need to make a friendly URL.
This is the URL where I enter the search values project_name and version
localhost:8080/demo_webpages_project/retrieval.html
This is where it takes me to on submitting the values
localhost:8080/demo_webpages_project/download.php?project_name=QT&version=3.1.2
I want the URL in the following form
localhost:8080/demo_webpages_project/download/QT/3.1.2
I tried doing this using .htaccess but not really finding a solution
The following is the code for the .htaccess file.
<IfModule mod_rewrite.c>
#Turn the engine on
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#attempt1
#RewriteRule ^(.*)/$ download.php?project_name=$1&version=$1
#RewriteRule ^/(.*)$ download.php?version=$1
#attempt2
#RewriteRule ^(.*)/(.*)$ download.php?project_name=$1&version=$2 [L,NC,QSA]
#RewriteRule ^(.*)$ download.php?version=$1
</IfModule>
I would be really grateful if someone could help me out with this. This is the first time I am working on something on this and am lost.
Thanks
Have this .htaccess inside /demo_webpages_project/:
Options -MultiViews
RewriteEngine on
RewriteBase /demo_webpages_project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ download.php?project_name=$1&version=$2 [L,QSA]
According to this article, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>

.htaccess WordPress ignore directory and remove index.php from that directory

So I know how to tell WordPress how to ignore a directory using the .htaccess file but I want to be able to remove the index.php from inside that directory too so I can access like this:
wordpresssite/directory/controller/function = wordpresssite/directory/index.php/controller/function
I've been trying but I'm clearly not very good at writing .htaccess files.
Thanks
My .htaccess file
The wordpress site is called smartronic and the subdirectory I want to ignore and remove index.php from is /mailin
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /smartronic/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(mailin|mailin/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /smartronic/index.php [L]
</IfModule>
# END WordPress
At the moment going to smartronic/mailin takes me to the xampp root page or a 404 page
To insert index.php into a fixed URL, you can use a simple RewriteRule
RewriteRule ^mailin/controller /mailin/index.php/controller [L]
If controller is variable, you need to capture this part with (...) and append it after the substitution with $1
RewriteRule ^mailin/(.*)$ /mailin/index.php/$1 [L]

hiding directory name in the URL

my site's directory is:
localhost/
includes/
includes/initialize.php
includes/user.php
includes/session.php
includes/database.php
public/
public/bootstrap/
public/css/
public/js/
public/dashboard/index.php
public/index.php
.htaccess(first htaccess file)
{
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [QSA,L]
</IfModule>
}
I'd like to have my url more neat, for example
when I type this: http://localhost I know it goes to http://localhost/public/
but the URL still is http://localhost
but the problem is when I type http://localhost/dashboard
unfortunately the URL will be http://localhost/public/dashboard/ on my browser.
I do not want to see the word public.
I think I should have another htaccess file under the public directory, but I do not know the suitable code for it.
Try this code in your DOCUMENT_ROOT/.htaccess file:
DirectoryIndex index.html index.php
RewriteEngine On
RewriteRule !^public/ /public%{REQUEST_URI} [NC,L]

Apache Rewrite URL, Works in WAMP, not in LAMP

I have a custom MVC app, where in it used IndexController naming convention, which I have built on WAMP. Today i tried it to put in to Linux (LAMP).. Strangely, it is giving error "page not found". Can anybody help. I am not good at mod rewrite,
Following is the code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
URL is
http://hostname/mvc/incident/add
Error is The requested URL /app01/users/public_html/mvc/index.php was not found on this server.
You need to put a / before index.php and some other stuff. Look at the example I got from Wordpress's one.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Try either adding:
RewriteBase /mvc/
just under the RewriteEngine on directive, or add a leading slash to index.php:
RewriteRule ^(.*)$ /mvc/index.php?request=$1 [L,QSA]
Hi I solved this issue by using error redirecting functionality. I put following code in my .htaccess file
ErrorDocument 403 /~renjith/mvc/index.php
ErrorDocument 404 /~renjith/mvc/index.php
and in index.php file i used, $_SERVER['REQUEST_URI'] to get thr URL with query strings
I think that ./htaccess configuration is correct. I solved it simply by adding this directive to my apache2.conf file on debian.
<Directory /var/www/html/your_app_folder>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted</Directory>

how can I set .htaccess for existing files and folders

Can any one help me in .htaccess.
My use case is, if there is index.php in the directory http://domain_name/user/ then load index.php else set rewriteRule to call forbidden.php.
I did a bit but not succeed yet. .htaccess code on my server is-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond ^(.*)$/index.php !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)$ forbidden.php?handle=$1
</IfModule>
I think there is an easier way to do what you want. There is no need for rewriting, simply define which page acts as the entry page. In the .htaccess file you can define:
DirectoryIndex index.php
ErrorDocument 404 /forbidden.html
Now, if anybody calls your directory http://www.example.com/user/, the page http://www.example.com/user/index.php will be shown.
If the file index.html does not exist, the server will return an error, so you can define an appropriate error page. With a leading /, you can define a single error page in the root directory, without the / it will look in the relative directory http://www.example.com/user/forbidden.php.

Categories